Hash Tables

Hash Tables (also known as Associative arrays or Dictionaries) are a type of array that allows the storage of paired Keys and Values, rather like a simple database table.

Unlike normal arrays where you refer to each element via a numeric index, the keys of a hash table can be strings. The Key values do still need to be unique, if you try to add the same key twice PowerShell will return an error ("item has already been added").

The built-in properties of a hash table include .Keys .Values and .Count

Create an empty Hash Table:

$array_name = @{}

Create a Hash table and immediately populate it with some values:

$array_name = @{key1 = item1; key2 = item2;...}

Example:

$usa_states=@{ CA="California";
  "NY" = "New York";
  "IL" = "Illinois";
  "NH" = "New Hampshire"}

Add items to a Hash Table

This is done using the .Add() method.

$usa_states.Add("GA", "Goregia")

Edit items in a Hash Table

This is done using the .Set_Item() method.

$usa_states.Set_Item("GA", "Georgia")

Combine Hash Tables

This is done by adding the hash variables with the + method, or appending with +=

$world_states = $usa_states + $india_states

Remove items from a Hash Table

This is done using the .Remove() method.

$usa_states.Remove("GA")

Retrieve items from a Hash Table

To display all contents just use the variable name.
$usa_states

Return just the New York key (the quotes are only needed here if the key contains spaces):
$usa_states.'NY'

Searching for items

This is done using the .ContainsKey or .ContainsValue methods.
$usa_states.ContainsKey('NY')
$usa_states.ContainsValue('New')

GetEnumerator

A hash table is a single PowerShell object, to sort, filter or work with the pipeline you can unwrap this object into it’s individual elements with the GetEnumerator() method.

$usa_states.GetEnumerator() | Sort-Object Name

When unwrapped with GetEnumerator, the hash table returns a collection of (DictionaryEntry) objects, so they must be processed by cmdlets that can accept a collection, or alternatively pass the objects individually via ForEach.

$hashtable.GetEnumerator() | ForEach-Object { … }
or
ForEach($item in $hashtable.GetEnumerator()) {Echo $item … }
ForEach($item in $hashtable.KEYS.GetEnumerator()) {Echo $item … }

Using @ as a SPLAT operator

New in PowerShell 2.0 is the ability to expand a hash table into a set of command line parameters.

First use @ to create a hashtable containing parameters to a cmdlet:
PS C:\> $params = @{path = "c:\demo"; Recurse= $true}

Then use @ to SPLAT the parameters:
PS C:\> dir @params

That will in effect run: dir -Path c:\demo -Recurse:$true

The paired keys and values become cmdlet parameters and values:

PS C:\> $params = @{year = 1980; Month = 5; day = 31}
PS C:\> get-date @params

The @ character is also used to create here strings.

“Nor shall you by your presence make afraid, The kingfisher, who looks down dreamily, At his own shadow gorgeously array'd” - Sir Edmund William Gosse

Related:

Group-Object - Group objects that contain the same value.
get-help about_array
Variables - Create/read variables
Exporting and importing Hash tables including Type information - Jeffery Hicks
Dealing with PowerShell Hash Table Quirks - The Scripting Guys


© Copyright SS64.com 1999-2013
Some rights reserved