December 11th, 2011

Combine Arrays and Hash Tables in PowerShell for Fun and Profit

Doctor Scripto
Scripter

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 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.