Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases of [ordered], the new PowerShell 3.0 feature

PowerShell 3.0 CTP1 introduces a new feature [ordered] which is somewhat a shortcut for OrderedDictionary. I cannot imagine practical use cases of it. Why is this feature really useful? Can somebody provide some useful examples?

Example: this is, IMHO, rather demo case than practical:

$a = [ordered]@{a=1;b=2;d=3;c=4}

(I do not mind if it is still useful, then I am just looking for other useful cases).

I am not looking for use cases of OrderedDictionary, it is useful, indeed. But we can use it directly in v2.0 (and I do a lot). I am trying to understand why is this new feature [ordered] needed in addition.


Collected use cases from answers:

$hash = [ordered]@{}

is shorter than

$hash = New-Object System.Collections.Specialized.OrderedDictionary

N.B. ordered is not a real shortcut for the type. New-Object ordered does not work.

N.B. 2: But this is still a good shortcut because (I think, cannot try) it creates typical for PowerShell case insensitive dictionary. The equivalent command in v2.0 is too long, indeed:

New-Object System.Collections.Specialized.OrderedDictionary]([System.StringComparer]::OrdinalIgnoreCase)
like image 241
Roman Kuzmin Avatar asked Oct 18 '11 04:10

Roman Kuzmin


People also ask

What is the use of hash table in PowerShell?

Hash tables are frequently used because they are very efficient for finding and retrieving data. You can use hash tables to store lists and to create calculated properties in PowerShell. And, PowerShell has a cmdlet, ConvertFrom-StringData , that converts strings to a hash table.

What is @{} in PowerShell?

@{} in PowerShell defines a hashtable, a data structure for mapping unique keys to values (in other languages this data structure is called "dictionary" or "associative array"). @{} on its own defines an empty hashtable, that can then be filled with values, e.g. like this: $h = @{} $h['a'] = 'foo' $h['b'] = 'bar'

What method can be used to empty all elements from a Hashtable PowerShell?

If you want to delete all the values from the hash table at once but retaining the hash table variable, you need to use the Clear() method.

How do I create a Hashtable in PowerShell?

To create a hash table in PowerShell, you'll use an @ symbol followed by an opening curly brace and a closing curly brace as shown below. Here you can see my hash table is now three lines with a key/value pair in the middle. It can also be represented on one line as well.


1 Answers

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.

like image 103
Josh Avatar answered Oct 18 '22 04:10

Josh