Create a Hash Table in PowerShell that Contains Hash Tables

Create a Hash Table in PowerShell that Contains Hash Tables

  • Comments 3
  • Likes

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:

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.

 Image of command output

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.

Image of command output

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.

 Image of command output

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 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Why not just make them in one go?

    $a = @{

       1 = @{

          'a' = 'A1';

          'b' = 'B1'};

       2 = @{

          'a' = 'A2';

          'b' = 'B2' } }

  • @James, you are absolutely correct, can definately make them in one step. The reason I chose not to do so here, is that I wanted to make sure everyone saw "all the steps" ... to make it easier for beginners to follow what exactly is going on. Thank you very much for your comment, it is a nice technique, and can make things easier when working at the command line.

  • What if I want to remove for example only "more people", from the Name Two, so that people would stay as a value, which way should I do that ?