What can Hash Tables do for you? (Part I – The Basics)

By | 2014-03-28

If you worked with Powershell for a while you have probably come across hash tables, and if not then it’s about time. In a series of posts I will try to explore some aspects of how to use and when to use hash tables.

From Wikipedia (http://en.wikipedia.org/wiki/Hash_table)

In computing, a hash table (also hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found

A hash table in its simplest form has a key and a value. The key must be unique and the value can be pretty much any type of object. To create a hash table just simply write:

If you want more keys you have mainly two choices.

Divide the pairs with a semi colon

Or divide the pairs with a line break

(You could also combine the two, and use both semi colon and a line break)

So the first hash table I came across was the one used in Select-Object

So what do we have here, we have a hash table with two keys Name, and Expression. Name holds the name of the Note property we will add. The property will hold the computed value of the code block in the Expression key.


So in this example we get a note-property with the name EditedWeekDay constructed by using the Expression code block “$_.LastWriteTime.DayOfWeek” in this case.

But now let’s start explore the hash table.

We create a small hash table as an examples

And if we look at the hash table now we can see that the order is probably not in the same order as we created our table. In my case I got the reverse order.


If order is of importance, you can make the hash table sorted in the same order as you made it, by using the key word ordered.

And now we get the topics in our original order.


Okay now we got a hash table, what can we do with it.
First of all, you can get access to what you have stored in the hash table and that is the point of hash tables.


So what useful properties do we have? Keys, Values and Count are the ones that I find most useful.


So this was all for now. The next post will focus on how to change hash tables and create new ones from objects

All code in this post

Leave a Reply