Wednesday, September 9, 2009

Learning PowerShell With Me ..

Day one

What is Powershell ?

Install Powershell

You have to download and install Powershell for windows XP and Vista, but its included in Windows 2008 and windows 7 by default. You just have to enable the feature in Windows 2008.

Lets Begin

start the powershell prompt.

image

A powershell prompt looks very much like a windows command prompt and almost all of the windows commands work well in the powershell command window with a few exceptions. For all of you unix lovers and that includes me, most of the unix commands work well inside powershell too.

Lets start with Dir

Dir command without any switches works exactly the same way it would in a command prompt.

Dir c:

image

but wait Dir c:\program files will not work without the quotes surrounding it, 'cause powershell treats “space” as the delimiter between command and its parameters. So, you should surrounding your path with quotes, to enable powershell to see the whole path properly

dir “c:\program files”

image

Powershell comes with a ton of help, that is readily accessible by using help <commandname>. The help context also supports wildcards.

Help <commandname> -full

will give you a very detailed explanation of the command with examples too.

Type in help dir

image

did you notice that the actual command is called Get-ChildItem. And it provides a bunch of parameter options like -recurse etc., This leads me to believe that “dir” or “ls” are probably aliases for the command Get-ChildItem. I'll probably find out as i go. Personally, i would think its better to stick with the actual cmdlets (yes, these commands are implemented as scripts called cmdlets), than with the aliases, 'cause it will help you get acquainted with the cmdlets and understand the code better as we move on, atleast for the duration of learning powershell. Once you get into actual implementation in real life, its up to. As for me, I'm going to stick with the cmdlets.

So to list a directory:

Get-ChildItem c:\perflogs

or

Get-ChildItem “c:\program files”

to recursively display a folder content

Get-ChildItem -recurse “c:\program files”

gives a long listing, use Ctrl +C, looks like that works in powershell as “Break” too.

PowerShell has the unique ability to navigate an hierarchical structure just like a file system viz., registry or active directory or storage systems. This is awesome, so you could navigate registry like below:

Get-ChildItem HKCU:

image

will list out the contents of HKEY_CURRENT_USER

Guess what the below command does:

Get-ChildItem HKCU: -recurse

you could also change your current location into the registry viz., below:

cd HKLM:\Software

or Set-Location HKLM:\Software
(yes, “cd” is the alias for cmdlet “Set-Location”, as i said, I'm going to stick with cmdlet names instead of aliases)

Same thing can be applied to Environment Variables also. Eg:

Set-Location ENV:

Get-ChildItem ENV:

Get-ChildItem ENV:\systemroot

image

Copy Items

“Copy” is another most used dos command. And the it works the same in powershell too. “Copy” is an alias for cmdlet “Copy-Item”.

Help Copy

or

Help Copy-Item

will reveal all the switches available for the cmdlet. Most commonly use switch would probably be “-recurse”

Delete Items

RD, Del or rm will work exactly as expected, but the underlying cmdlet for all this is “Remove-Item”

you could use rd or del or rm to achieve the same result eg:

rd c:\temp\test.txt

or

del c:\temp\test.txt

or

Remove-Item c:\temp\test.txt

you could also use the same “-recurse” switch to recursively delete folders.

Read contents of a file

Most of us are used to “TYPE” or “CAT” commands to achieve this. The same works in PowerShell too. Eg:

type c:\temp\test.txt

cat c:\temp\test.txt

or use the cmdlet directly

Get-Content c:\temp\test.txt

PowerShell Drives (PSDrives)

As we have seen, powershell lets you navigate registry, storage systems, environment variables etc., using simple navigation commands that we are used to. It can do that, 'cause it loads them as Psdrives or PowerShell Drives.

To see a list of all these PowerShell drives, which you can navigate, type in the below:

Get-PSdrive

image

This will show you all the drives that PowerShell has loaded. The power of navigating through certificate stores, Environment Variables and functions is amazing.

Did you notice that PS (PowerShell) has loaded “alias” as a drive. So lets see all the aliases that PS has built-in by listing the contents of the drive.

Get-ChildItem alias:

image

WOW!, that's a nice list of aliases that can be used. It would be handy to have a printed list (cheatsheet)
of these aliases that you can pinup at your desk. I'll make one up.

Cmdlet Naming syntax

The beauty of the long names is that, they have a consistent naming syntax. Each command has a verb-Singularnoun, syntax. ie., the cmdlet starts with a verb viz., get, remove etc., and ends with the singularnoun, ie., ChildItem, content, Psdrive etc., Stress on the word Singular, 'cause none of the commands actually end in plural, there is no Get-ChildItems or Get-Contents. Cmdlets nouns always are Singular.

Eg: issue the command below to see a list of all cmdlets available to powershell

Get-Command

image

Notice how all the cmdlets start with a verb, and end with a singular noun. None of them have a plural form.

The reason, this is done, is that, it is easier to search of a particular cmdlet, when needed.

Hope you have already started to ride with me and will continue .......