Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C++
Article

An Introduction to Windows Power Shell (Monad)

Rate me:
Please Sign up or sign in to vote.
3.14/5 (4 votes)
16 May 20067 min read 55.3K   24   7
This articles explains the basics of working with Windows Power Shell (previously code named 'Monad')

Introduction

<st1:personname w:st="on">Microsoft’s new Command Line Interface (CLI) called Windows PowerShell (previously called <st1:personname w:st="on">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.
<o:p>

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

<o:p>

Cmdlet<o:p>

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.<o:p>

<o:p> 

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.<o:p>

<o:p> 

The first Cmdlet<o:p>

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.<o:p>

<o:p> 

Some more get Cmdlets<o:p>

Now that we know about the get-help and get-childitem Cmdlet let’s try to use the get-childitem Cmdlet. <o:p>

<o:p> 

<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><o:lock v:ext="edit" aspectratio="t"><o:p>

<o:p>Sample screenshot
 

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

<o:p> 

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

<o:p>
Sample screenshot 

<o:p>

<o:p> 

If you want to know what processes are running in your computer you can use get-process.<o:p>

<o:p>
Sample screenshot 

<o:p>

<o:p> 

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

<o:p> 

Following are a few more get Cmdlets – <o:p>

<o:p> 

Cmdlet<o:p>

Description<o:p>

get-date<o:p>

Displays the current system date<o:p>

get-drive<o:p>

Displays the current drive<o:p>

get-command<o:p>

Displays a list of all the Cmdlets<o:p>

get-eventlog<o:p>

Displays the detail of entries in the event log<o:p>

get-UICulture<o:p>

Displays the active culture<o:p>

get-UICulture<o:p>

Displays the active UI culture<o:p>

<o:p> 

Set Cmdlets<o:p>

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. <o:p>

<o:p> 

Alias<o:p>

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. <o:p>

<o:p> 

To get a list of all the existing aliases you can use get-alias.  <o:p>

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 - <o:p>

set-alias time get-date<o:p>

Now we can use time to display the system date – <o:p>

<o:p> 

Sample screenshot <o:p>

<o:p> 

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 -   <o:p>

remove-item alias:time<o:p>

<o:p> 

Clear Console<o:p>

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.   <o:p>

<o:p> 

Change your prompt<o:p>

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 –<o:p>

Function Prompt { "$(get-location)> " } <o:p>

<o:p> 

To display both PSH (short for PowerShell) and location use - <o:p>

Function Prompt { "PSH $(get-location)> " } <o:p>

<o:p>
Sample screenshot 

<o:p>

<o:p> 

Redirecting Output<o:p>

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<o:p>

get-process > ProcessList.txt<o:p>

<o:p> 

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

<o:p> 

Some more basic Cmdlets<o:p>

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.<o:p>

<o:p> 

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 – <o:p>

new-item . -name newtextfile.txt -type file<o:p>

<o:p>
Sample screenshot 

<o:p>

<o:p> 

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 – <o:p>

new-item . -name newtextfile.txt -type file -value "This file is created using PSH"<o:p>

<o:p>
Sample screenshot 

<o:p>

<o:p> 

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

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

<o:p> 

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 - <o:p>

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

You can also give the file with a new name rather than the existing name- <o:p>

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

<o:p> 

Similarly, to move the file or folder, you can use the move-item Cmdlet -   <o:p>

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

<o:p> 

To delete a file or folder you can use the remove-item Cmdlet. For example – <o:p>

remove-item -Path "C:\Documents and Settings\TestFolder\TextFile.txt"   <o:p>

<o:p> 

<o:p>

<o:p>

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


Written By
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 Pin
logicchild14-Nov-07 13:03
professionallogicchild14-Nov-07 13:03 
QuestionWhy is it so verbose? Pin
Shane Poznikoff16-May-06 4:40
Shane Poznikoff16-May-06 4:40 
AnswerRe: Why is it so verbose? Pin
Robert Ensor22-Aug-06 20:22
Robert Ensor22-Aug-06 20:22 
AnswerRe: Why is it so verbose? Pin
peterchen23-Aug-06 4:50
peterchen23-Aug-06 4:50 
AnswerRe: Why is it so verbose? Pin
thompsonson216-Nov-06 5:43
thompsonson216-Nov-06 5:43 
GeneralFormatting and content Pin
NormDroid16-May-06 1:41
professionalNormDroid16-May-06 1:41 
GeneralRe: Formatting and content Pin
Aniruddha Chakrabarti16-May-06 2:54
Aniruddha Chakrabarti16-May-06 2:54 
Thanks for the comment. I thought I'll cover the very basics first and then will move to advanced features. I will cover some advanced features in next part of the article.

Aniruddha
(caniruddha@hotmail.com)

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

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