[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Making better use of HashTables in PowerShell

Published on
3,467 Points
Last Modified:
This article will help you understand what HashTables are and how to use them in PowerShell.

What are HashTables?

HashTables are used for saving Key-Value pairs, where each Key must be unique, whereas their corresponding values may not be unique.


Here is the syntax for creating a HashTable:

$ht = @{"Name"="Savindra"; "Age"=33; "Department"="IT"; "City"="Hyderabad"}

When you see the output of the above command by giving $ht, you will notice that the order of the output is not same as we have provided.


With PowerShell Version 3.0 onwards you can preserve the order of properties of the HashTable by using [Ordered] with the HashTable like this:

$ht = [Ordered]@{"Name"="Savindra"; "Age"=33; "Department"="IT"; "City"="Hyderabad"}

This will preserve the order in which you have supplied the values in the HasTable. This time you will notice that the order of properties in input and output is same:


Why are HashTables useful?

Since HashTables can store any kind of values including objects/arrays/HashTables etc., they are great for using within your scripts for providing an output, especially when you want to have some kind of hierarchical output. You can easily convert the HashTables into PSCustomObjects before writing them as output. I will show you two different ways of converting the HashTables into PSCustomObject which is a good way for passing the output to next command in the pipeline.

1. Longer way:

$objOut = New-Object -TypeName PSCustomObject -Property $ht

This will give us a variable which will return a PSCustomObject as shown below:PipeLineOutput.png

However, this same result can be achieved using an easy and shorter way by type casting the HashTable while creating it.

2. in single step:

$objOut = [PSCustomObject][Ordered]@{"Name"="Savindra"; "Age"=33; "Department"="IT"; "City"="Hyderabad"}

I hope this is informative for you. Please let me know if you have any queries.

Thank you for reading this post.
1 Comment
LVL 74

Expert Comment

Note that starting with PS 4 you do not need to use [Ordered] with [PSCustomObject], as the sequence gets preserved already.
And there is a lot more to say about hashtables, of course ...

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Join & Write a Comment

Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month