Use cases of [ordered], the new PowerShell 3.0 feature
This is actually particularly useful for creating on-the-fly objects sorta like how you would use ExpandoObject in C# or dynamic objects in JavaScript. The problem with doing this in prior versions of PowerShell is that @{}
(which becomes a regular HashTable
) loses your ordering of keys so it's a huge PITA to display these to the screen.
Consider this.
foreach ($row in import-csv blah.csv) {
# In v3: $obj = [Ordered]@{
$obj = @{
Name = $row.Name
Exists = Test-Path $row.FileName
OtherProp = 123
Blah = "derp"
Timestamp = Get-Date
}
New-Object PSObject -Property $Obj
}
In PowerShell v2, the column order is unpredictable because the Hashtable
does not retain the order of the keys. In PowerShell v3, if you used the [Ordered]
type, the key order is retained, making the quick and dirty PowerShell syntax almost as convenient as JSON for quickly building up object structures without all the overhead and performance issues associated with Add-Member
or Select-Object
.
Not coincidentally, there is another addition to PowerShell v3. Similar to [Ordered]
you can actually specify [PSCustomObject]
instead. This will create an actual PSObject
right from the start instead of requiring a separate call to New-Object
. I can't say for sure but I am pretty sure that [Ordered]
was a side effect of the changes they made to the parser to make that happen. If they were just doing a conversion from a regular Hashtable
, there'd be no way to restore the original order of the keys.
First I will start by asking - Why not have them?
I can think of a use case in my project where we use Powershell scripts for build and deploy and yml for config ( using https://github.com/scottmuc/PowerYaml )
The config from yml is read in as hashtables. The tasks are specified in the yml like deploy to database, deploy to iis, deploy service and so on. I would like to do the database deploy and then website deploy so that I can avoid iisreset afterwards. Currently I have to explicitly look at that. Now, I can have an Ordered hash table and specify the deploy database first and hence it happens first.
Snippet:
function Convert-YamlMappingNodeToHash($node)
{
$hash = @{}
$yamlNodes = $node.Children
foreach($key in $yamlNodes.Keys)
{
$hash[$key.Value] = Explode-Node $yamlNodes[$key]
}
return $hash
}
Now the $hash =@{}
become $hash=[ordered]@{}
I don't know how this means for a bloated product. OrderedDictionary is there in .NET ( I have lots of use cases for it in programming ) and they have just added an accelerator for it.