PowerShell 3.0 [Ordered] Hash Table
What’s new with hash tables in PowerShell 3.0 is they can be[Ordered]. With just one command you can sequence values thus: [Ordered]@{"Key1" = "Value 1";"Key2" = "Value 2; etc …"}.
- Hash Table Refresher
- PowerShell 3.0 [Ordered] Command
- Further Research of PowerShell’s Hash Tables
- Sorting Number Values in a Hashtable
- PowerShell Array
♦
Hash Table Refresher
Arrays are useful, but only if you have a series of single values. Hash tables extend arrays by allowing you to have data in the format of Key = Value.
$Countries = @{"Wales" = "Daffodil"; "England" = "Rose"; "Scotland" = "Thistle"; "Ireland" = "Shamrock"}
$Countries
Note 1: Observe the semi-colon; between each pair.
Note 2: If need be, you could display just the Keys with $Countries.Keys.
Note 3: Speech marks are only required if the key, or its value, contains a space.
PowerShell 3.0 [Ordered] Command
The new feature of PowerShell v 3.0 hash tables is that you can sort the key using the [Ordered] command see this example:
Clear-host
$Countries = [Ordered]@{"Wales" = "Daffodil"; "England" = "Rose"; "Scotland" = "Thistle"; "Ireland" = "Shamrock"}
$Countries
This [Ordered] method sorts on the value, e.g., Daffodil, Rose, Shamrock and Thistle.
Alternatively, you can use the old .GetEnumerator() method to sort on Name.
Clear-Host
$Countries = [ordered]@{"Wales" = "Daffodil"; "England" = "Rose"; "Scotland" = "Thistle"; "Ireland" = "Shamrock"}
$Countries. GetEnumerator() | Sort-Object Name
Hash Table Results
Name Value
——— —–
England Rose
Ireland Shamrock
Scotland Thistle
Wales Daffodil
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Further Research of PowerShell’s Hash Tables
Hashtables are sometimes called dictionaries; whatever the name, and whatever the spelling, you can list the full set of methods and properties by appending | Get-Member.
$Countries = @{"Wales" = "Daffodil"; "England" = "Rose"; "Scotland = "Thistle"; "Ireland" = "Shamrock"}
$Countries | Get-Member
One benefit of creating a hash table is that you can add data, for example:
$Countries.Add("France", "Cockrell")
Note 4: The .Add method requires (Parenthesis) not {curly} brackets.
Footnote: This research solves the mystery of why PowerShell doesn’t have a .Delete method; the reason is because it consistently uses .Remove for this task.
Sorting Number Values in a Hashtable
Here is an alternative to using the [Ordered] command; we need Sort-Object if the values are number rather than text.
Clear-Host
$Tennis = @{
'Mens Singles' = 50;
'Boys Singles' = 8;
'Girls Singles' = 7;
'Mens Doubles' = 33;
'Womens Singles' = 40;
'Womens Doubles' = 25;
'Mixed Doubles' = 16
}
#$Tennis | Sort-Object value # Will not work
$Tennis.GetEnumerator() | Sort-Object Value -Descending
Learning Points:
Observe the importance of .GetEnumerator.
$Tennis | Sort-Object Value, just would not work, you need that .GetEnumerator() method.
Summary of PowerShell v 3.0 Ordered Hash Table
Another new feature in version 3.0 is [Ordered] hash tables. Thanks to this one word [ordered] you can achieve a quicker and better solution for sequencing text values than was possible in PowerShell v 2.0.
If you like this page then please share it with your friends
See more Microsoft PowerShell v 3.0 examples
• PowerShell 3.0 • What’s New in PowerShell 3.0 • PowerShell v 3.0 ISE • PowerShell Home
• Get-NetAdapter • Disable-NetAdapter • Enable-NetAdapter • Get-NetIPConfiguration
• PowerShell Network Cmdlets • PowerShell 3.0 Logon Script • PowerShell Show-Command