Click here to Skip to main content
Click here to Skip to main content

An Introduction to Windows Power Shell (Monad)

, 16 May 2006
Rate this:
Please Sign up or sign in to vote.
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Aniruddha Chakrabarti
Web Developer
United States United States
Aniruddha works as an Architect and Designer on Enterprise Architecture and .NET. He has over 8 years of experience in designing n-tier Enterprise Business Application using Microsoft .NET and Windows DNA architecture. He has been working in .NET and has been passionately involved with .NET since the beta days in 2001. His interests include .NET, Object Oriented Analysis and Design, Design patterns, Frameworks, eXterme Programming and other Agile Methodologies. He has worked with major international clients like ING Netherlands, Natioanale Netherlanden, ING Poland, Sogeti Nederland, Cendant, US and CP 7 11. Currently he is working in Traget Services India as Technical Architect and manages the .NET Center of Excellence in India. Prior to this he has worked in Microsoft, Cognizant Technology Solutions and NIIT Technologies. He can be reached at caniruddha@hotmail.com

Comments and Discussions

 
GeneralRe: article Pinmemberlogicchild14-Nov-07 13:03 
QuestionWhy is it so verbose? PinmemberShane Poznikoff16-May-06 4:40 
AnswerRe: Why is it so verbose? PinmemberRobert Ensor22-Aug-06 20:22 
AnswerRe: Why is it so verbose? Pinmemberpeterchen23-Aug-06 4:50 
AnswerRe: Why is it so verbose? Pinmemberthompsonson216-Nov-06 5:43 
GeneralFormatting and content Pinmembernorm.net16-May-06 1:41 
GeneralRe: Formatting and content PinmemberAniruddha Chakrabarti16-May-06 2:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 16 May 2006
Article Copyright 2006 by Aniruddha Chakrabarti
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid