Guy recommends :
Free Permissions
Analyzer Tool

Solarwinds Free Download of Permissions Analyzer

View the effective permissions for a folder or shared drive. Free download try it now!


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 and 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 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 (;). 

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" }

SolarWinds Firewall Browser Solarwinds Free Firewall Browser

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.

Guy recommends that you download a copy of the SolarWinds free Firewall Browser.

Example 2: 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 5: This example uses Group-Object's -AsHashTable parameter to leverage data supplied by WMI.

Note 6: See more on the use of PowerShell's $_ variable.

Example 3: 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 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"}.

See more on PowerShell Event Logs »

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

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2013 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: