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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-224.png" data-lazy- height="643" src="data:image/svg xml,” width=”783″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-225.png" data-lazy- height="543" src="data:image/svg xml,” width=”686″>

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”}


$colorList

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-226.png" data-lazy- height="292" src="data:image/svg xml,” width=”791″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-227.png" data-lazy- height="545" src="data:image/svg xml,” width=”680″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-228.png" data-lazy- height="322" src="data:image/svg xml,” width=”805″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-229.png" data-lazy- height="318" src="data:image/svg xml,” width=”834″>

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 )


$ageList

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-230.png" data-lazy- height="265" src="data:image/svg xml,” width=”780″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-231.png" data-lazy- height="293" src="data:image/svg xml,” width=”755″>

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


$ageList

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-232.png" data-lazy- height="265" src="data:image/svg xml,” width=”773″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-233.png" data-lazy- height="291" src="data:image/svg xml,” width=”743″>

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

}


$ageList

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-234.png" data-lazy- height="267" src="data:image/svg xml,” width=”771″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-235.png" data-lazy- height="281" src="data:image/svg xml,” width=”749″>

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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-236.png" data-lazy- height="305" src="data:image/svg xml,” width=”749″>

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

}

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-237.png" data-lazy- height="265" src="data:image/svg xml,” width=”735″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-238.png" data-lazy- height="172" src="data:image/svg xml,” width=”744″>

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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-239.png" data-lazy- height="298" src="data:image/svg xml,” width=”750″>

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

}


$person

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-240.png" data-lazy- height="268" src="data:image/svg xml,” width=”703″>

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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-241.png" data-lazy- height="234" src="data:image/svg xml,” width=”711″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-242.png" data-lazy- height="252" src="data:image/svg xml,” width=”868″>

Conclusion

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

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/talha-150×150.jpg61132d52b3fe6.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.