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 and
they work on Key = Value pairs.
Before I deal with hashtables, here are some simple array examples.
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}
Hashtables are 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 (;).
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.
Here is an utility where you can review firewall settings such as
access control lists (ACL), or troubleshoot problems with network
address translation (NAT).
Other reasons to download this SolarWinds Firewall Browser include
managing requests to change your firewall settings, testing firewall
rules before you go live, and querying settings with the browser's
powerful search options.
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 5: This example uses Group-Object's -AsHashTable parameter to leverage
data supplied by WMI.
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.
Note 7: The syntax has a few surprises; a) There is no
hyphen before the parameter. b) It uses the = (equals sign) and not -eq.
Also remember the overall PowerShell hashtable format @{Filter="criteria"}.
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!
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
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
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.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.