PowerShell Hashtable

Introduction To PowerShell Hashtable

I think of a PowerShell's hashtable as a super array.  Whereas arrays are one dimensional, hashtables have multiple keys.  Hash tables make it easier to work with the underlying connections, they work on Key = Value pairs.

Topics for PowerShell’s Hashtable

 ♣

Introduction to PowerShell Arrays and Hashtables

Before I deal with hashtables, here are some simple examples of arrays.

Array @(Parenthesis)

You could think of an array as a one dimensional hashtable, however, the analogy is not perfect, and is let down by the fact that PowerShell arrays use (parenthesis) whereas hashtables use {braces}.

$CompArr =@("Jasmine", "Louise", "Longhorn")

Note 1: The ‘At’ symbol (@) is mandatory for introducing both arrays and hashtables.

Hashtable @{Curly Braces}

A hashtable is also know as an associative array, a dictionary, Key = Value pairs.

$GuyHashTable = @{Key = Value pairs}

Note 2: You need that @ ‘At’ before the curly brackets.  While PowerShell normally uses -eq, hashtables require the old fashioned = sign between the key and its value.  Most hashtables will have multiple keys, these are separated by semi-colons (;). 

Example 1: Simple PowerShell Hashtables

# Simple PowerShell Hashtable
$CompHash = @{Jasmine ="Win7"; Louise="Vista"}

Note 3: By assigning the hashtable to a variable you can access the value of individual keys.  Naturally, this is more useful where you have lots of Key = Value pairs.

Example 1b: Interrogating the Hashtable

# PowerShell Hashtable Example
$CompHash = @{Jasmine ="Win7"; Louise="Vista"; Mary="XP"}
$CompHash.Louise
Write-Host "There are" $CompHash.count "keys"

Note 4: In addition to accessing the value by its $variable.key, you can return a count of all the keys.

Example 1c: A Hashtable Featuring Books

# PowerShell Hash table
Clear-Host
$Book = @{ISBN = 1843560295 ; title = "Lost"; author = "Guy, Thomas" }

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 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

Example 2: Changing Values in a Hashtable

The scenario: We have a hash table, but we wish to change some of the values from 5 to 3.  We would also like to sort the results.

Clear-Host
$Status = @{
   Single = 5;
   Married = 5;
   Divorce = 6;
   Bigamist = 12;
   Polygamist = 33
}
$Update = $Status.GetEnumerator() | ? {$_.Value -eq 5}
$Update | % { $Status[$_.Key]=3 }
#$Status | Sort-Object value # Will not work
$Status.GetEnumerator() | Sort-Object value

How to Sort a Hashtable
Learning Point: Observe the importance of .GetEnumerator.

Note 5: $Status | Sort-Object value, just would not work, you need that .GetEnumerator() method.

Example 3: PowerShell  -AsHashTable to List Services That Should Have Started

The purpose of this real-life hashtable example is to identify which Windows services are set to Auto start, but are in fact stopped.  -AsHashTable is a parameter of Group-Object.

# PowerShell -AsHashTable with Windows services
Clear-Host
$HashService = Get-WmiObject Win32_Service | Group-Object State -AsHashTable
$HashService.Stopped | Where {$_.StartMode -eq "Auto"} |
Format-Table Name, StartMode, State -auto

Note 6: This example uses Group-Object’s -AsHashTable parameter to leverage data supplied by WMI.

Note 7: See more on the use of PowerShell’s $_ variable.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Example 4: Get-WinEvent -FilterHashTable

Get-WinEvent has a -FilterHashTable parameter so that you can refine the output.  Example 3 shows how to filter on just two properties, it would be easy to add more criteria.

Clear-Host
Get-WinEvent -MaxEvents 100 -FilterHashtable @{Logname="System"; ID="50036"}

Note 8: The syntax has a few surprises; a) There is no hyphen before the Logname parameter.  b) It uses the = (equals sign) and not -eq.  Also remember the overall PowerShell hashtable format @{Filter="criteria"}.

See more on PowerShell Event Logs »

Help About_Hashtables

Amongst PowerShell's many 'About' files is one dedicated to Hashtables.  It reminds us that object types in HashTables can use any .NET object type in their keys or values.

Clear-Host
Help About_h*  # How I discovered this help file.
# Help About_Hashtables

See more PowerShell Help About »

Summary of PowerShell Hashtable

It’s the ‘At’ that introduces a hashtable.  Whereas arrays are one dimensional, hashtables can have multiple keys.  Hashtables make it easier to work with the underlying connections and they work on Key = Value pairs.

If you like this page then please share it with your friends

 


See more PowerShell examples for syntax advice

PowerShell Syntax  • PowerShell Array  • PowerShell Array String Conversion  • Plist

Get-Date  • PowerShell Quotes  • PowerShell variables  • RegEx  • PowerShell functions

• PowerShell Tutorials  • Conditional operators  • PowerShell Hashtable   • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.