Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

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:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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 hypen 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

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 Tutorials  • Syntax  • PowerShell functions  • Plist

Get-Date  • Quotes  • PowerShell variables  • RegEx 

Conditional operators  • 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

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.