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.
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
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
hypen before the parameter. b) It uses the = (equals sign) and not -eq.
Also remember the overall PowerShell hashtable format @{Filter="criteria"}.
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 one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.