Intro to Powershell

I’m not sure if a lot of people out there are using powershell.  If you are working in a technical position I think that you should definitely investigate the possibility of using it to manage day to day tasks.

Microsoft has been requiring all of their software releases to include an interface for powershell.  This has been especially evident in the newer operating systems as well as Exchange and Active Directory.

What I hear most often is, “Well it seems complicated and I can’t program anyway; its really hard.” In actual fact its really easy; much easier than driving a car or riding a bike.  Don’t believe me?  Try it.

From any Windows 7 (or newer windows computer) try clicking on start and then type in “powershell” into the little run box and press enter.

Start - Run - Powershell

So… thats it… powershell should open and you can start typing in commands.  If you are familiar with DOS commands they should work here (almost) like they always have.  (try dir, cd, copy etc..).  Also, probably due to a lot of complaining, you can use unix type commands (try ls, cd, cp).  Both should work!  Finally!! Forward slash AND backslash work… which… makes sense.

So you are probably thinking to yourself, “Big deal, you said this was easy, that stuff is really hard.” Did you know that all of the commands in powershell are actual verb-noun pairs?  In fact, even the short commands like the ones I listed above are actually aliases for verb-noun pairs.  For example, dir or ls are ACTUALLY aliases for a command called Get-ChildItem; Don’t believe me?  Type “alias dir” and you should see something like:

PS C:\data> alias dir
CommandType     Name                            Definition
-----------     ----                            ----------
Alias           dir                             Get-ChildItem

Hmm… Thats interesting.

So what is going on here?  Why is that useful?  Well, the answer is really quite simple.  Those old commands were made back in the olden days when computers couldn’t even store a program with a name greater than 8 characters!  Like 20 years ago! The nerds of back in the day love their commands and they can still use them and newbies can jump right in and get their feet wet.  The basic fact that they are verb-noun pairs means that you can GUESS at what the commands actually are.  For example type get-  (that’s get and a dash) and start pressing the tab key in powershell.  Did you notice that starts suggesting commands to you?  Pretty cool huh?  Whats even more important is that you can read the noun and probably GUESS at what each command does; even without a book.  Other cool verbs to try are (set-, start-, export-, stop-, select- and many more!! You can even add your own).

Alright, so whats next?  Well… I hope I’m keeping you interested and you are learning quickly.  After all, thats the point of this exercise.  In my opinion, you need to learn how to use 1 command to actually learn all of powershell; but understanding about 4 of them would actually take you to the next level.  So what is that command?

help

No, that wasn’t a typo, I want you to learn help!  Help has many names in powershell.  Its also known as Get-Help (verb-noun pair 😉 and has an alias of man for the linux nerds; for normal humans “man” actually means “manual”.

So when you type get-help, help tells you how to use help! I know it all seems a bit redundant but you need to wrap your head around the concept of how help works.  EVERY single command in powershell should be in the manual and hence… you don’t need the book.  If you want to see examples of help for a particular command, you would add “-examples” to the end of your help command and the shell will actually tell you what to type in. The help command would actually tell you more about this if you wanted to see the FULL information about help… to do that type:

help -full

At this point you should get the same information that you got before plus another 3 pages of information about help and using help.  I STRONGLY recommend you read this if you want to learn powershell in any capacity.

The next thing you need to learn are the following commands:

get-psdrive
get-command
get-member
get-alias

How do you learn those commands?  Don’t make me smack you! Type:

get-help get-psdrive -full

Get it?

Its all pretty easy, I’ve already given you everything you actually need to know in order to “learn powershell”. To make it a bit more fun, how about a real example?

Now lets say your friend is bugging you. You don’t like it, but its your friend and you don’t want to actually hurt their feelings.  What is a good way to annoy them?  How about opening 30 copies of notepad on their computer?  That would be great!! It would annoy them a little bit and you get to use your powershell skills to make the job easy.  Type the following command:

for ($x=0; $x -lte 30; $x++) { Start-Process notepad }

Press enter and voila!  You just opened 30 copies of notepad.  Sweet! So what is going on there?  How did I do that.  Well the command above is actually something called a loop.  The first part says set the letter X to the number 0, make sure X is less-than-equal (-lte) than 30 and each time add 1 to X ($x++).  The part in between the curly brackets is what we want to do 30 times.  In this case we specified a command to start notepad.  Pretty nifty and fun too. If you want to learn more about for loops try:

 get-help about_for

You might think to yourself, that was actually not a nice thing to do to your friend and you don’t want to have all those notepads open on their computer and actually you want to clean it all up for them.  Well… in that case you would type something like:

Get-Process notepad | Stop-Process

Wow!! Cool, it just closed all those notepads in one command!! How did that work?  Well… the command above introduces a concept of something called “the pipeline”.  Yes, I know the pipeline is something that surfers strive to ride, and good for them; in this case, it means something completely different.  The “pipe” is that “|” character.  It basically says, take everything from the last command and send it to the next command.  The command above instructs the computer to find all the processes running on the computer that have notepad running and take those OBJECTS and pass them to the stop-process command.  You could of course just typed “get-process notepad” and it would have done something different.

The pipeline is a pretty cool concept. Imagine all of those verb-noun pairs and linking them all together.  The possibilities are endless; I hope you benefited from my powershell introduction. Obviously some of the commands in powershell are dangerous; so be careful.  I wonder what the stop-computer command does?

Happy powershelling!

~JCF