Combine Arrays and Hash Tables in PowerShell for Fun and Profit

Combine Arrays and Hash Tables in PowerShell for Fun and Profit

  • Comments 1
  • Likes

Summary: Take your Windows PowerShell scripting to the next level by combining hash tables and arrays.

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are anxiously counting down the days until the first ever Pittsburgh PowerShell Users Group meeting. The event is quickly selling out; therefore, if you want to attend, you will need to sign up for the meeting. The reason for the reservation is that space at the Pittsburgh Microsoft Office is limited. The meeting will be a lot of fun.

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:

Today, I want to look at creating an array that contains hash tables. Suppose I create a couple of hash tables, such as the ones that are shown here.

$a = @{"one"=1;"two"=2}

$b = @{"three"=3;"four"=4}

If I want to create an array with these two hash tables, it is as simple as using the equality operator, and assigning them to a variable by using standard array techniques. This command is shown here.

$c = $a,$b

The view of this array of hash tables appears to be a bit strange—it looks like I am looking at a hash table. This is shown here.

PS C:\> $c

 

Name                           Value

----                           -----

two                            2

one                            1

three                          3

four                           4

The cool thing is that I can index into the elements of my array by using square brackets as shown here.

PS C:\> $c[0]

 

Name                           Value

----                           -----

two                            2

one                            1

Because each element of the array contains a hash table, I can index into the array, and then use “hash table kinds of commands” such as querying the Keys or Values properties or using the Item method. These commands are shown here.

PS C:\> $c[0].values

2

1

PS C:\> $c[0].keys

two

one

PS C:\> $c[0].item("one")

1

The commands that create two hash tables, add the hash tables to an array, and then index into the array to work with the hash tables are shown in the following image.

Image of command output

If I am only interested in the keys that the hash tables contain, I can pipe the array to the Select-Object cmdlet. This technique is shown here.

PS C:\> $c | select keys

 

Keys

----

{two, one}

{three, four}

Although the output does contain the keys, it is not the best output. I can clean things up a bit by using the ExpandProperty parameter. This command is shown here.

PS C:\> $c | select -ExpandProperty keys

two

one

three

four

That is about all there is to working with an array of hash tables. Join me tomorrow as I begin a new week with a special guest blog post by Karl Mitschke.

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
  • Hi Ed,

    you are covering a pretty challenging topic!

    Arrays of Arrays, Hashes of Hashes, Array of Hashes and Hashes of Arrays ...

    It is certainly possible to build even more complex data structures with powershell, as with nearly all other programming languages. If everything is descended from an rather abstract object type and we may build "aggregates" of objects somehow ... we are free to stick all possible components together!

    Believe it or not, it's a very good idea to get used to these "constructs". You may not like to work with them at first glance, but they can be extremely useful if the data your are dealing with corresponds to these structures! If you mess around with simpler structures you might end up building additional program logic to glue separate information bits together. This will finally NOT reduce the overall complexity behind the real world objects and you may have additional difficulties holding together the pieces of the puzzle.

    Klaus.