65.9K
CodeProject is changing. Read more.
Home

An Introduction to Windows Power Shell (Monad)

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.14/5 (4 votes)

May 16, 2006

7 min read

viewsIcon

55859

This articles explains the basics of working with Windows Power Shell (previously code named 'Monad')

Introduction

Microsoft’s new Command Line Interface (CLI) called Windows PowerShell (previously called Microsoft Command Shell or MSH, code named 'Monad') gives System Administrators the power that was missing in the existing command line tools. Windows PowerShell is entirely different from the existing traditional shells like Windows CMD Shell or the UNIX Shells like SH, KSH, CSH, and BASH.

  • Windows PowerShell uses an object model based on the powerful and secure .NET platform.
  • List of built in commands in much larger that any existing shell

Cmdlet

A Cmdlet (pronounced as Command-let) is the built in commands available in Windows PowerShell. You can compare it with the commands in traditional shells. Cmdlet is the smallest unit of functionality in Windows PowerShell. Every Cmdlet is expressed in terms of verb-noun pair which is very intuitive and similar to the way we talk in day to day life. For example if we want a cup of coffee we ask for get a cup of coffee. So if you want to know the list of sub directories under a directory the Cmdlet would be get-childitem. So get-childitem is an example of a Cmdlet. Note the hyphen between the verb and noun words.

 

If you want to know or get some information you have to use the get verb along with what you want (noun). Similarly if you want to change or set some information you have to use the set verb along with what you want (noun). Other than get and set the verbs used in Cmdlets are – add, clear, combine, export, format, import, new, write etc.

 

The first Cmdlet

So after installing Windows PowerShell (it requires .NET Framework) what is the first Cmdlet you should try? Like the ubiquitous Hello World program used as the starting point for learning any new programming language, in Command Shell world it is the help command. So what would be the Cmdlet for getting help? Yes you guessed it right – get-help. get-help takes the name of the Cmdlet on which you want help as parameter. If you do not pass any parameter to get-help it will display the help of get-help Cmdlet. So open the Windows PowerShell window and type get-help get-childitem to get a complete help about the get-childitem Cmdlet.

 

Some more get Cmdlets

Now that we know about the get-help and get-childitem Cmdlet let’s try to use the get-childitem Cmdlet.

 

Sample screenshot
 

As expected get-childitem has displayed all the files and folders present in C:\.

 

If you want to know what is the current folder you can use get-location, which you display the path of your current location.


Sample screenshot 

 

If you want to know what processes are running in your computer you can use get-process.


Sample screenshot 

 

As you have understood till, Windows PowerShell Cmdlets are very intuitive and very easy to understand compared to traditional shell commands.

 

Following are a few more get Cmdlets –

 

Cmdlet

Description

get-date

Displays the current system date

get-drive

Displays the current drive

get-command

Displays a list of all the Cmdlets

get-eventlog

Displays the detail of entries in the event log

get-UICulture

Displays the active culture

get-UICulture

Displays the active UI culture

 

Set Cmdlets

The Set Cmdlets helps you to set some settings or change something. For example if you want to change the current location you can use set-location. Similarly if you want to change the current system date you can use set-date.

 

Alias

Now, suppose you are an Unix Administrator and are more used to Unix Shell commands. So what would you do? Probably you have noticed already that most of the existing DOS commands like dir, cls, copy works in Windows PowerShell also. That’s because these commands are mapped to the new Cmdlets, so that you can use the existing commands also. Though over the time, surely you will be using the new verb-noun form of Cmdlets for all your day to day work. You can create an alias which is like a pointer to an original Cmdlet. Alias also helps you reduce the amount of typing. So you can use your favorite dir or ls instead of using get-chilitem, which yields the same result as get-chilitem.

 

To get a list of all the existing aliases you can use get-alias. 

You can also create your own aliases by using get-alias. For example you want to create an alias time which should display the current system date. So we will create an alias for time which will point to get-date Cmdlet -

set-alias time get-date

Now we can use time to display the system date –

 

Sample screenshot 

 

If you now use to get-alias to display list of aliases you fill find an entry for time. You can remove an alias by using remove-item -  

remove-item alias:time

 

Clear Console

By now you have used a lot of Cmdlets and the console is looking quite messy. So you want to clear the console. You can use the clear-host Cmdlet to clear the console. You can also use cls since it is also an alias for clear-host.  

 

Change your prompt

You can change the Windows PowerShell prompt as you could so in any shell by using the prompt function. To create a prompt that is similar to the DOS prompt use

Function Prompt { "$(get-location)> " }

 

To display both PSH (short for PowerShell) and location use -

Function Prompt { "PSH $(get-location)> " }


Sample screenshot 

 

Redirecting Output

To redirect output from the default console to a file or printer Windows PowerShell uses the redirection operator “>” similar to other shells. So to save the list of processes in a text file called ProcessList.txt you will use

get-process > ProcessList.txt

 

The “>” redirection operator will remove any exiting content of the file. To append the existing content of the file use the “>>” operator instead.

 

Some more basic Cmdlets

Now let’s work with some basic Cmdlets before proceeding ahead. In everyday life we work a lot with files and folders. Now let’s check how to create files and folders.

 

To create a new file or folder we have to use the new-item Cmdlet. To create a file named newtextfile.txt at the current location we have to use –

new-item . -name newtextfile.txt -type file


Sample screenshot 

 

Go to your current folder. You will find that a file with the name you specified is created. Since the file does not contain any text it’s Length is 0. If you want to put some content into the file while creating it use the -type option and provide the content –

new-item . -name newtextfile.txt -type file -value "This file is created using PSH"


Sample screenshot 

 

To create a new folder named TestFolder under C:\ Documents and Settings you can use -

new-item –Path “C:\Documents and Settings” –Name TestFolder –Type Directory 

 

To copy a file or folder you can use the copy-item Cmdlet. For example to copy the previously created newtextfile.txt from C:\ to C:\ Documents and Settings\TestFolder you can use -

copy-item -Path "C:\newtextfile.txt" -Destination "C:\Documents and Settings\TestFolder"

You can also give the file with a new name rather than the existing name-

copy-item -Path "C:\newtextfile.txt" -Destination "C:\Documents and Settings\TestFolder\textfile.txt"

 

Similarly, to move the file or folder, you can use the move-item Cmdlet -  

move-item -Path "C:\newtextfile.txt" -Destination "C:\Documents and Settings\TestFolder"

 

To delete a file or folder you can use the remove-item Cmdlet. For example –

remove-item -Path "C:\Documents and Settings\TestFolder\TextFile.txt"  

 

Conclusion

So, as you can see Windows PowerShell uses very intuitive, easy to use and consistent yet very powerful language. In the next part of the article we will discuss some more advanced features of Windows PowerShell.