Friday, August 04, 2006

PowerShell'ed

During the last couple of months I've spent quite some time on getting the grips on PowerShell (PS). As written earlier, PowerShell will be important, but expect it to take some time and effort to get it 'in the fingers'.

When you first look at it, you may ask yourself - hey - what's the big news in the examples you see? Most things can easily be done with other well-known tools. What so funny about - say - creating 10 folders -
PS> 1..10 % { md folder$_ }
when I could do it with
CMD> for /l %i in (1,1,10) do md folder%i

And what is the big advantage in starting a service with
PS> start-service bits
when I could do it with
CMD> net start bits

Of course, restart is nicer
PS> restart-service bits
But this does the job as well
CMD> net stop bits & net start bits

But as you start try to do REAL stuff you realize the potential. How about keeping the logs for the last 10 days, named with the date? That is not easy at all with CMD and I often created an self-contained script to do the job -
CMD> set script=%temp%\x.vbs
CMD> echo script line 1 > %script%
CMD> echo script line 2 >> %script%
CMD> cscript %script%
With PS you can do it right away with code like -
PS> $logbase="$env:temp\my.log"
PS> $fivedaysold=$logbase+[system.datetime]::now.adddays(-5).tostring("yyyyMMdd")
PS> $fivedaysold
D:\DOCUME~1\user\LOCALS~1\Temp\my.log20060730

So, if you want to learn PS, do not just use it once in a while. Expect that you will spent much time on learning it - but stop using CMD as much as possible and realize that you can combine all your tricks and (programming) skills by combining command line stuff with COM, WMI and .Net stuff - right from the command line. In this way, you will learn along the way.

Be powershelled yourself!

No comments: