How to Use Hashtables in PowerShell

Hashtables are used to store values that contain a key. For example, you have a locker; only if you have the keys will you open the lock. The same is the case with hashtables. Let’s look at another scenario. Suppose you are searching for a book in a library, and you start by picking up the book closest to the front door, then the next book, and so on. Finding the book you want would take quite a long time.

Alternatively, you can check in the library index, which will give you an approximate location of the book. The functionality of a hashtable is similar to a library index. Hashtables enable programs to retrieve data predictably and quickly. Technologies like Google, which analyzes data in terabytes and makes it searchable in a second, would be impossible without implementing hashtables in their code.

What are Hashtables in PowerShell?

Hashtables are the most useful data structures in PowerShell. A hashtable is made up of a series of “key” and “value” pairs. These tables can be utilized in many different ways. Utilize them as data lookup tables, allowing you to query the hashtable based on its key and retrieve the associated value. It would help to locate the container corresponding to the “key” to retrieve its “value”.

In practice, utilizing keys as a data lookup is efficient and can help cache. If you have a large amount of reference data and you do not want to query it in a database regularly, you can use the hashtable to cache the result and save it for accessing it more quickly. These tables do not permit the addition of “NULL” values, which helps you save memory.

This article will show you how to use hashtables in PowerShell. So, let’s begin!

How to Use Hashtables in PowerShell

First of all, open up your “Windows PowerShell ISE” by searching it manually in the search box:

From the file, select the option “New” and create a new PowerShell script:

Now, create a simple hashtable by declaring it in your PowerShell script. Specify your hashtable name with the “$” variable and define its value inside of the curly braces “@{ }”.

$colorList = @{ID = 1; Shape = "Square"; Color = "Blue"}

In our script, we have declared a hashtable named “$colorList” having the keys “ID, Shape, Color” with their respective values: “1, Square, Blue”.

Save this script as “testfile1.ps1” and run this PowerShell script:

The execution of the “testfile1.ps1” will show you the following output:

You can also define “keys” and their “values” as separate variables. After that, you use the “add()” function to add keys and their values in the hashtable.

$key = 'Dave'
$value = 36
$ageList.add( $key, $value )
$ageList.add( 'Alex', 9 )

In this script, we have passed “Dave” and “36” as “$key” and “$value” as variables in the “add()” function. In contrast, “Alex” and “9” are passed directly in the same function. Both functions calling statements will add keys and values in the hashtable.

How to Access Values of Hashtable in PowerShell

You have seen the method of adding keys and their values in the hashtable. Similarly, you can access the key with its respective value.

$ageList = @{}
$key = 'Kevin'
$value = 36
$ageList[$key] = $value
$ageList['Alex'] = 9

How to Declare Hashtable with Values in PowerShell

PowerShell provides you two methods for creating a hashtable. The first one is to create an empty hashtable and then later add the “keys” and “values” in it. In another method, you can declare a hashtable with its “keys” and values” at once. The below-given example will demonstrate this concept to you:

$ageList = @{
    Kevin = 36
Alex  = 9

How to Iterate Hashtables in PowerShell

A hashtable is a combination of keys and values; it is iterated over in a different way than an array or a regular list of objects. The first thing you have to notice in the below-given command is that we are piping our hashtable using the pipe operator [“|”]; the pipe operator treats the hashtable like one object. Whereas, the “Measure-Object” command is utilized to extract the property values of the hashtable.

> $ageList | Measure-Object

The execution of the above-given command will display the following output on your PowerShell:

You can also use the “GetEnumerator()” method for the purpose of iteration over the hashtable. Use this function with the “ForEach-Object”, it will show you the key and its paired value one after another.

$ageList.GetEnumerator() | ForEach-Object{
    $message = '{0} is {1} years old!' -f $_.key, $_.value
    Write-Output $message

How to Check the Number of Hashtable Values in PowerShell

The “.count” property is used to calculate the total number of values in your hashtable. You can use this property with your hashtable in the following way:

> $ageList.count

How to Order Hashtable key/values in PowerShell

When you want your hashtable keys and values to be in the order in which you define them, you can specify the “[ordered]” keyword while declaring your hashtable in the script:

$person = [ordered]@{
    name = 'Kevin'
age  = 36

Execute this “testfile1.ps1” script, and it will print out the keys and their values in order by which we have defined them in our script:

Without the [ordered] option, the script will show you the following output:


Hashtables are quite significant in PowerShell that can be utilized in more advanced administrative and scripting scenarios. PowerShell’s use of hashtable also demonstrates how helpful PowerShell output as objects can be. For just about any type of data, hashtables are an effective way to store like key/value pairs.

This pattern helps to locate the container that corresponds to the “key” to retrieve its “value”; In practice, utilizing keys as a data lookup is efficient and useful in caching. In this article, we have shown you how to use hashtables in Powershell including the method of creating a hashtable, declaring and accessing its values, checking, ordering hashtable values.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.