A PowerShell hash table is data structure of key/value pairs. A key/value pair is essentially a set of two elements that are related in some manner. In its simplest form, a hash table is just a way to store one or more sets of item names and item values.
Avatar

To get started, let's first create a hash table. To create a hash table in PowerShell, you'll use an @ symbol followed by an opening curly brace and a closing curly brace as shown below.

@{}

This is the simplest hash table that can be created. Even though this technically is a hash table, it's not much use because it contains no elements (key/value pairs) inside it. To add a key/value pair, add a name and a value. Typically, the name is a string value, and the value is just about anything. A key/value pair is added to a hash table like this:

@{
    SomeKey = 'SomeValue'
}

Here you can see my hash table is now three lines with a key/value pair in the middle. It can also be represented on one line as well.

@{ SomeKey = 'SomeValue' }

I tend to add each key/value pair on separate lines to keep them easy to read. You can add as many elements to this hash table as you like.

@{ 
    SomeKey = 'SomeValue'
    SomeKey2 = 'SomeValue2'
    SomeKey3 = 'SomeValue3'
}

PowerShell will then output the hash table to the console.

Hashtable in console

Hashtable in console

Notice that each of the keys is unique. This is required in a hash table. It's not possible to add two keys with the same name. Below I've defined the SomeKey2 key twice.

Duplicate keys

Duplicate keys

We've just talked about creating a hash table and adding key/value pairs at creation time. It's also possible to add keys to an already created hash table in a number of different ways, either through dot notation, enclosing the key in brackets, or by using the Add() method. Each approach has the same effect.

$hashTable = @{}
$hashTable.Foo = 'Bar'
$hashTable['Foo'] = 'Bar'
$hashTable.Add('Foo','Bar')

These keys can be read individually as well. By just leaving off the "= 'Bar'" part, this will return the value of the Foo key.

$hashTable.Foo

Perhaps you need to remove keys from a hash table? This can be done with the Remove() method and using the key name as the first argument.

$hashTable.Remove('Foo')

We've now created a hash table, added keys at creation time, added keys to existing hash tables, and removed keys from them. These are all common actions. Another common task is just figuring out what keys or values exist. Every hash table has Keys and Values properties that enumerate all of the keys and values in the hash table.

$hashTable.Keys
$hashTable.Values

Perhaps you'd like to read each of the keys and their values at one time. This is known as iterating through the hash table. There are a couple of ways to do this, but the easiest way is to use the GetEnumerator() method.  The GetEnumerator() method essentially splits each key/value pair so that the individual names and values can be read separately.

Here's a method using a foreach statement and the GetEnumerator() method to iterate over each key in a hash table.

Subscribe to 4sysops newsletter!

$hashTable = @{
    Key1 = 'Val1'
    Key2 = 'Val2'
    Key3 = 'Val3'
}
foreach ($key in $hashTable.GetEnumerator()) {
    "The key $($key.Name) is $($key.Value)"
}
Using the GetEnumerator method

Using the GetEnumerator method

Hash tables are an excellent way to store like key/value pairs for just about any kind of data. As you've seen, they are pretty easy to manipulate and provide a structured way of handling information in code.

avatar
2 Comments
  1. Avatar
    jaaron 3 years ago

    I tried doing this but i get this error!

     

    At line:3 char:8
    + foreach($obj in $InstalledSoftware){write-host $obj.GetValue('Display …
    +        ~
    Missing '=' operator after key in hash literal.
    At line:3 char:8
    + foreach($obj in $InstalledSoftware){write-host $obj.GetValue('Display …
    +        ~
    The hash literal was incomplete.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEqualsInHashLiteral

     

     

    I need to do this Please create a PowerShell script to fetch the list of installed software through Registry keys. And create a Hash table to hold the information (DisplayName & DisplayVersion).

  2. Avatar
    Will Ullathorne 3 years ago

    Great article.

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account