7/31/2010
LifeCycle Solutions - Home ( the software development blog )
 

<July 2006>
SunMonTueWedThuFriSat
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

Subscribe to this feed:

RSS 2.0 | Atom 1.0 |CDF

On this Page:
PowerShell Rocks





Friday, July 28, 2006

I have to confess, I just didn't get PowerShell.  .NET at the command prompt was interesting, but why not just write a console application to do the same stuff?  I'd still prefer that for many tasks, but think of PowerShell as a replacement for the command line and batch files.  Probably the coolest thing about it, though, is the way commands can be glued together.  You're no longer constricted to working with strings-  type 'dir' and you get a list of FileInfo objects formatted as string.  You can pipe those FileInfo objects to another command, which  works against those objects:

dir | sort Length -desc| select Name, Length

The above sorts the list by the length property, and selects the name and length property for display.  Keep in mind these are .NET FileInfo (and DirectoryInfo) objects.  You can do the same for any .NET type, and access any method or property just as you would in VB or C#:

dir | foreach { $_.GetType() }

Where it gets cool is when use those other types .NET provides:

$webclient = new-object System.Net.WebClient
$diggFeed = $webclient.DownloadString("http://digg.com/rss/containertechnology.xml")


So, now the $diggFeed variable contains digg.com's rss feed as a string.  Cool, but it's more useful as XML:

$diggFeed = [xml]($webclient.DownloadString("http://digg.com/rss/containertechnology.xml"))
$diggFeed.rss.channel.item | select title


Note that the xml is "serialized" and usable as though it were an object.  So, I can play with the xml data like this:

$diggFeed.rss.channel.item | where { $_.title -like "*.NET*"} | select title

And viola- 3 lines of code to get all the .NET posts on digg.  Keep in mind we could then copy those to our own rss or do just about anything else with them. 

Anyway, get PowerShell and use it instead of the old C:\.   For more stuff you can do, check out the MSDN PowerShell site.  I suspect we'll be seeing more of it used in conjuction with Visual Studio to assist in build scripts and configuration tasks.






Posted by Daniel Root

© 2006 LifeCycle Solutions, LLC | All Rights Reserved