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:

1 |
@{Key="Value"} |

If you want more keys you have mainly two choices.

Divide the pairs with a semi colon

1 |
@{Key1="Value1"; Key2="Value2"} |

Or divide the pairs with a line break

1 2 3 4 |
@{ Key1="Value1" Key2="Value2" } |

(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

1 2 3 |
$Files = Get-ChildItem "C:WindowsSystem32driversetc" $Files | Select-Object Name,LastWriteTime,` @{Name="EditedWeekDay";Expression={$_.LastWriteTime.DayOfWeek}} |

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

1 2 3 4 5 |
$Topics = @{ TheBasics = "Some starting points on hash tables" TheNextStep = "Convert your objects in to hash tables" TheSpeedOfHashTables = "Why Hash Tables should be your new best friend" } |

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*.

1 2 3 4 5 |
$Topics = [ordered]@{ TheBasics = "Some starting points on hash tables" TheNextStep = "Convert your objects in to hash tables" TheSpeedOfHashTables = "Why Hash Tables should be your new best friend" } |

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.

1 2 3 4 5 6 7 8 9 10 11 12 |
$Topics.TheBasics #Some starting points on hash tables $Topics."TheBasics" #Some starting points on hash tables $Topics["TheBasics"] #Some starting points on hash tables $CurrentTopic = "TheBasics" $Topics.$CurrentTopic #Some starting points on 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
$Topics = @{ TheBasics = "Some starting points on hash tables" TheNextStep = "Convert your objects in to hash tables" TheSpeedOfHashTables = "Why Hash Tables should be your new best friend" } $Topics $Topics = [ordered]@{ TheBasics = "Some starting points on hash tables" TheNextStep = "Convert your objects in to hash tables" TheSpeedOfHashTables = "Why Hash Tables should be your new best friend" } $Topics #Use your hash table $Topics.TheBasics #Some starting points on hash tables $Topics."TheBasics" #Some starting points on hash tables $Topics["TheBasics"] #Some starting points on hash tables $CurrentTopic = "TheBasics" $Topics.$CurrentTopic #Some starting points on hash tables # hash table properties $Topics.Keys $Topics.Values $Topics.Count |