Sunday, June 27, 2010

My experiences with PowerShell

I was not into scripting before but when I started using powershell, I loved it so much that now I can't live without it :).Its simple yet so powerful and with 2.0 release it has become even more powerful with the addition of remote administration.

PowerShell out of the box provides lot of CommandLets (cmdlets) which can executed to get the work done. PowerShell is built on top of .Net, so basically you can do anything you in it , that you can do in VisualStudio and it can hosted in different applications like WPF app, Windows Forms etc.

Any data that can be modelled as hierarchical data or flat data, can be used with the general command lets that powershell provides.For Eg : Drives, Environment Variables,

In this post I am going to just list the normal commandlets I use, later in next blogs I will show how to create CommandLets using c# code.

Commandlets that I used often

1) Get-Help "CommandLet name"   -- To get help about how to use the cmdlets, what parameter and arguments
2) Get-Member -InputObject  "Object"
3) Copy-Item  fromlocation tolocation
4) Get-ChildItem "Path"  -recurse  -- this is very useful to get the child elements of a Hierarchical data eg : Certs,Registry etc
5) Get-Process
6) Stop-Process -whatif
7) Get-Content -- to read the contents and show it on the PowerShell Host
8) Set-Content -- to set some content back to the source
9) Remove-Item - to remove any item
10) Get-WmiObject "Win32_XXXX"
11) Where-Object  {"Expression"} -- From Filtering the output, you can pipe the output of different cmdlets and then filter the output by using Where-Object.
12) Select -Object {"Expression}  -- Used for Projection

Accessing .Net Classes and imvoking member functions

13) "String".Substring()   -- Accessing function of an .Net Object
14) [System.Guid]::NewGuid()   -- Accessing static members of .Net Classes

Declaring Arrays and HashTable in PowerShell

15) $x  = @{ "key1"="Yash"; "key2" = "Patil"}  -- Declaring HashTable
16) $x = @("Yash","Patil")  -- Declaring array

To Set the Drives installed on the machine, Setting Different drives

17) Get-PSDrive
18) Set-Location  -- To set the location to different drives like Environment, Drives etc.

19) Select-String -- Replacement for Unix's Grep
20) Invoke-Command -ComputerName { Expression }

PowerShell is very powerful and can be used with Remote Machines as well i.e the same cmdlets that you run on localMachine can be used with remote machine.

A good ebook to learn more about PowerShell :

Love the power PowerShell gives with command lines and the uniform way of accessing different Data-Stores.