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:

$Hash_array_name = @{}

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

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

Note that unlike Arrays which use normal () parenthesis, a Hash table must be created using curly brackets { }
A useful mnemonic to remember this is to think of the H in Hash with it's horizontal bar }H{ and compare with the bracket {H}


$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, this will not overwrite an existing entry.

$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")

This is actually the default action so you can also do
$usa_states."GA" = "Georgia"

If the entry does not already exist it will be automatically added.

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.


Retrieve items from a Hash Table

To display all contents just use the variable name.

Return just the New York key (the quotes are only needed here if the key contains spaces):

Searching for items

This is done using the .ContainsKey or .ContainsValue methods.


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 { … }
ForEach($item in $hashtable.GetEnumerator()) {Echo $item … }
ForEach($item in $hashtable.KEYS.GetEnumerator()) {Echo $item … }

Using @ as a SPLAT operator

Splatting expands a hash table into a set of command line parameters.

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

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

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

Store an Array inside a Hash table

An array can be stored as part of a hash table. This is possible because hash tables use the semi-colon as key-value pair separators, which leaves the comma available to create an array:

PS C:\> $hash = @{ customer = "IBM"; order_items = 64,65,67 }
PS C:\> $hash.order_items

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 PowerShell Commands:

Group-Object - Group objects that contain the same value.
get-help about_array
Variables - Create/read variables
Dealing with PowerShell Hash Table Quirks - The Scripting Guys

Copyright © SS64.com 1999-2019
Some rights reserved