Posey's Tips & Tricks
How To Create Multi-Dimensional Arrays in PowerShell
A quick primer on one of the most useful tools in PowerShell scripting.
Over the years, I have built a lot of home-brew utilities by using PowerShell scripting. In doing so, I find myself constantly making use of arrays.
Working with arrays is a fairly straightforward process, but what a lot of people don't realize is that PowerShell does not limit you to using basic, run-of-the-mill arrays. You can also create multi-dimensional arrays.
Before I show you how multi-dimensional arrays work, I want to spend a few minutes talking about normal PowerShell arrays for the benefit of anyone who might not be familiar with them. Arrays are a mechanism that allows you to store a whole list of values within a single variable. Each of the items in the array can be accessed individually. Let me give you an example.
Let's pretend that for whatever reason, I wanted to create an array containing a list of colors. I could do so by declaring a variable (which stores the array) and assigning multiple values to the variable. Each of these values is separated by a comma. Here is an example of what the variable declaration might look like:
Items within the array are referenced by position. The first array item, in this case red, is in position 0. The second item, which is yellow in this case, is in position 1. The remaining items in the array are also referenced by sequential position numbers. Therefore, if you wanted to see the item that is in position 0 of the $Colors array, you could use this command:
The position number is enclosed in brackets after the variable name. You can see the variable declaration in Figure 1, as well as what happens when I display the entire variable contents and what happens when I display the contents of a single array position.
Oh, and in case you are wondering, arrays do not have to be static. You can easily add, remove and modify the contents of an array. If I wanted to add another color to my $Colors array, for example, I could use this command:
$Colors += "Purple"
You can see what this looks like in Figure 2.
Now that I have given you a really quick primer on regular arrays, let's take a look at multi-dimensional arrays. A multi-dimensional array is kind of like a table in that the array consists of rows and columns.
Let's suppose that for some crazy reason I wanted to create a multi-dimensional array containing a few of the marshmallow colors and shapes in Lucky Charms cereal. To do so, what I would have to do is to create a single array consisting of two separate lists. Here is what the command might look like:
$Marshmallows = @(("Pink","Yellow","Orange","Green","Blue"),("Hearts","Stars","Moons","Clovers","Diamonds"))
You can see this type of variable declaration in Figure 3, as well as a listing of what the variable actually contains.
What if I wanted to retrieve a value from the array? Well, the key to doing so is to understand that the rows and columns are numbered. In this case, my array has two separate lists, and therefore two separate rows. Row 0 is the first list that I declared (the colors), while Row 1 is the second list that I declared (the shapes).
The individual items within each list are also ordered, just as they are in a simple array. Therefore, each array item is referenced by two numbers: the row number and the position within the list. A position of 1,2 would refer to the third item (because the first item is numbered 0) on the second list, which in this case is Moons. If I actually wanted to display the contents of this position in PowerShell, I would use this command:
You can see an example of this in Figure 4.
That's just a quick demonstration of how you can set up multi-dimensional arrays in PowerShell. In a script, you would not typically reference array positions by manually specifying them as I have done here. Arrays are normally referenced either through loops or computationally.
Brien Posey is a 19-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.