Summary: Learn how to work with hash tables that contain other hash tables in Windows PowerShell.
Microsoft Scripting Guy, Ed Wilson, is here. Well, it is a typical winter day here in Charlotte, North Carolina in the United States. I am not talking about a nice, cool, sunny day with cobalt blue skies streaked by fluffy cotton candy clouds—nope, that is the “chamber of commerce” picture. A typical winter day around here seems to be cool, damp, and drizzled with rain—at least that is the way it has been for the past several days. That is OK; I figure we need all the rain we can get, as long as it does not come all at once.
I thought I would get up early and work on my presentation for the Pittsburgh PowerShell User Group meeting. The Scripting Wife and I will be there for the first meeting, and we are both really looking forward to it. Space is limited at the meeting; therefore, it is essential to sign up early to obtain a ticket (the tickets are free).
This past week, I talked about working with arrays. Although Windows PowerShell makes working arrays very easy, there are still lots of little things that need special attention. The five articles in the series devoted to working with Arrays in Windows PowerShell are listed here:
- In the first post, Learn Simple Ways to Handle Windows PowerShell Arrays, I discussed creating arrays, indexing into arrays, and two techniques for walking through an array.
- In the second post, Add, Modify, Verify, and Sort Your PowerShell Array, I discussed adding, modifying, or verifying values in a Windows PowerShell array, and two easy techniques for sorting the array.
- In the third post, Find the Index Number of a Value in a PowerShell Array, I talked about working with arrays to determine the index number of a given value. In addition, I covered working with one half of the array at a time.
- In the fourth post, I answered a question that was sent to scripter@microsoft.com about reading a CSV file and building distinguished names on the fly by using array notation. The Read a CSV File and Build Distinguished Names on the Fly by Using PowerShell post was really cool, and it presented a practical way to use arrays.
- The last post was Easily Create and Manipulate an Array of Arrays in PowerShell, and I discussed working with an array of arrays.
One of the cool things to do with Windows PowerShell is to create a hash table that contains other hash tables. Creating a hash table that contains other hash tables offers a decent amount of flexibility, but it can become a bit confusing. Today, I thought I would spend a little time to help alleviate some of that confusion.
To create a hash table, I use the “hash table” operator that consists of an at sign (@), and a pair of curly brackets. Remember, that a hash table consists of one or more key/value pairings. For example, I create a hash table that consists of three items. This hash table is stored in the hashtable variable as shown here.
$hashtable = @{1="one";2="two";3="three"}
When I examine the hash table, I see that the numbers are the name (key) elements, and the words are the associated value. This is shown here.
PS C:\> $hashtable
Name Value
---- -----
3 three
2 two
1 one
If I create an additional hash table, and I attempt to add it to the hash table that is stored in the hashtable variable, an error occurs. The error is shown in the following image.
Receiving an error is certainly not my desired result. I then decide to use the += operator to add the two hash tables together. This technique is shown in the following image.
Unfortunately, merely adding the two hash tables together, does not allow me to have a hash table of hash tables. It creates a larger hash table and automatically incorporates the elements from the new hash table into the old hash table.
To create a hash table that contains other hash tables requires remembering that a hash table is comprised of key/value combinations. Therefore, I use the Add method, but I first add a key value, then I store the hash table in the value portion. The syntax is shown here.
$newhash.Add("hashtable",$hashtable)
In the following image, I create two hash tables. I then create an empty hash table and call the Add method to add the two hash tables to the newly created one.
When I have a hash table that contains other hash tables, I can use the Values property to view all the properties that are contained in the hash table. This command is shown here.
PS C:\> $newhash.Values
Name Value
---- -----
5 five
4 four
3 three
2 two
1 one
In addition to viewing the values, I can view the keys in the hash table by using the Keys property. This command is shown here.
PS C:\> $newhash.Keys
h
hashtable
I can also use the Item method to retrieve a specific hash table. To do this, I use the Key property, which displays in the output as a Name column as shown here.
PS C:\> $newhash.Item("h")
Name Value
---- -----
5 five
4 four
PS C:\> $newhash.Item("hashtable")
Name Value
---- -----
3 three
2 two
1 one
In addition to retrieving the hash tables that are stored in the newhash hash table via the Item method, I can also retrieve specific items from the stored hash tables. This technique is shown here.
PS C:\> $newhash.Item("hashtable").item(1)
one
PS C:\> $newhash.Item("h").item(4)
four
It is possible to work with the hash tables that are stored in the hash table in the same way that one works with other hash tables. For example, suppose I want to create two hash tables, and I store those two hash tables in a new hash table. The code to do this is shown here.
$a = @{"dog"="dog food";"cat"="cat food"}
$b = @{"people"="people food"}
$c = @{"one"=$a;"Two"=$b}
I can use the Keys property to retrieve the keys from one of the hash tables, and I can use the Values property to look at the values. This technique is shown here.
PS C:\> $c.Item("two").keys
people
PS C:\> $c.Item("two").values
people food
In addition, I can use the Add method to add key/value pairs to one of the hash tables. This appears here.
PS C:\> $c.Item("two").add("more people","more people food")
PS C:\> $c
Name Value
---- -----
Two {people, more people}
one {dog, cat}
Well, that is about all there is to working with hash tables that contain other hash tables.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy