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.

Adam Bertram

Adam Bertram is a 20-year IT veteran, Microsoft MVP, blogger, and trainer. Adam is the founder of the e-learning tech screencast platform TechSnips. Catch up on Adam’s articles at adamtheautomator.com, or follow TechSnips on Twitter at @techsnips_io.

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:

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.

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.

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.

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

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.

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.

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.

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.

Join the 4sysops PowerShell group!

1+

Users who have LIKED this post:

  • avatar
Share
0 Comments

Leave a reply

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

*

© 4sysops 2006 - 2019

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