I enjoyed reading Rob Conery’s post The Reasons I Like Ruby. He has a nice example where he wants to import a list of countries from an online file.
Version that follows the Ruby one
$url = "http://openconcept.ca/sites/openconcept.ca/files/country_code_drupal_0.txt" $wc = New-Object Net.WebClient ($wc.DownloadString($url)).Split("`r`n") | ForEach { $countryCode, $countryName = $_.Split('|') New-Object PSObject -Property @{ CountryCode = $countryCode CountryName = $countryName } }
Shorter version using PowerShell’s ConvertFrom-Csv
$url = "http://openconcept.ca/sites/openconcept.ca/files/country_code_drupal_0.txt" $wc = New-Object Net.WebClient $wc.DownloadString($url) | ConvertFrom-Csv -Delimiter "|" -Header CountryCode, CountryName
Results
- This is all out of the box PowerShell
- Note the New-Object Net.WebClient. Tapping into the .NET framework
- Notice the 5th line of the first version, the one with the .Split(‘|’), showing off PowerShell’s ability to assign variables in succession
Both versions return an array of objects (.NET objects), each object has two properties. CountryCode, CountryName
# Results CountryName CountryCode ----------- ----------- AF Afghanistan AL Albania DZ Algeria AS American Samoa AD Andorra AO Angola AI Anguilla AQ Antarctica
It’s safe to say that with C# and System.Net it’s just a bit longer. Not that much longer – but you’d have to have a pretty practiced hand to get through the "machine noise" that begins to creep in. – Rob Conery





{ 2 comments… read them below or add one }
Just heard your interview on RunAs Radio. It’s good to hear that there are some other devs out there who are interested in PowerShell. It seems like an untapped resource to me. I wonder if that isn’t because Microsoft have been pushing PowerShell as a tool for admins at the detriment of treating it like another.NET language.
I have a background project to use PowerShell as the configuration language for Castle Windsor. I’d like to get the benefit of an imperative language at configuration time just as the users of Binsor do (Binsor is Windsor configuration using the Boo language). The use of PowerShell for this sort of thing seems obvious but only a small number of developer-focussed PowerShell projects seem to be out there – the most obvious one being PSake.
After reading Rob’s blog entry, yours made me giggle. Very cheeky!
Thanks Damian. I agree PowerShell is an untapped dev resource.
There are a bunch of devs out here doing PowerShell work. PowerShell is an interesting balance between IT Pro tool and System Dev tool.
It belongs in your toolbox regardless. We’ll see where V3 takes us.
I’ve got Ayende’s Boo book and I like it.
You probably have seen these. This is a short list and these are devs doing PowerShell.
PowerShell Community Extensions – http://pscx.codeplex.com/
PowerGUI – http://www.powergui.org/index.jspa
PowerShell Plus – http://www.idera.com/Products/PowerShell/