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:

$Colors="Red","Yellow","Orange","Green"

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:

$Colors[0]

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.

Figure 1: This is how you create and reference a normal array.

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.

Figure 2: I have added an extra item to the array.

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.

[Click on image for larger view.] Figure 3: This is how you declare a multi-dimensional array.

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:

$Marshmallows[1][2]

You can see an example of this in Figure 4.

[Click on image for larger view.] Figure 4: This is how you reference an individual item within a multi-dimensional array.

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.

About the Author

Brien Posey is a 16-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.

Featured

  • Office Mobile Apps To End as Microsoft Highlights New Office App

    Microsoft plans to end support for Windows 10 Mobile applications on Jan. 12, 2021, according to a Friday announcement.

  • Is Microsoft Finally Reinventing Office?

    Microsoft is testing out a new technology called "Fluid Framework." It could mean that Brien's dream of one Office app to rule them all might soon become reality.

  • Azure Active Directory Connect Preview Adds Support for Disconnected AD Forests

    Microsoft on Thursday announced a preview of a new "Cloud Provisioning" feature for the Azure Active Directory Connect service that promises to bring together scattered Active Directory "forests."

  • Microsoft Defender ATP Gets macOS Investigation Support

    The endpoint and detection response (EDR) feature in Microsoft Defender Advanced Threat Protection (ATP) has reached the "general availability" stage for macOS devices.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.