Share via

Windows PowerShell Tip of the Week

Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know.

Accessing Values in an Array

Despite their glamorous lifestyle (for example, in the past year alone Scripting Guys Jean Ross and Greg Stemp have been everywhere from Orlando, FL to … well, Orlando, FL) the Scripting Guys are, at heart, simple people with simple tastes. Take Windows PowerShell, for example. Ask 100 people what they like best about Windows PowerShell and you’ll likely get 100 different answers, most of them dealing with sophisticated new capabilities such as access to the .NET Framework. Ask any of the Scripting Guys what they like best about Windows PowerShell and you’re likely to get the same answer: the cool ways that Windows PowerShell lets you access values in an array.

If you’re an old hand at VBScript then you’ve come to look upon arrays with a certain feeling of dread. Why? Well, for one thing, you can’t just echo back the value of any array; that’s going to result in a “type mismatch” error. For example, take a look at the following VBScript script:

x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Wscript.Echo x

Is that going to work? You already know the answer to that, don’t you? No, it’s not going to work. Instead, you need to set up a For Each or For Next loop in order to get at the value of x:

x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

For Each y in x
    Wscript.Echo y

That’s not particularly hard, but it does require some extra work on your part. On top of that, you might need to check beforehand to ensure that x really is an array in the first place (which results in even more work on your part). After all, trying to loop through something that isn’t an array will also result in an error:

x = 1

For Each y in x
    Wscript.Echo y

In this case all we’re going to get back is the message “Object not a collection.”

With Windows PowerShell it’s a different story. For example, consider the following Windows PowerShell script:

$x = 1,2,3,4,5,6,7,8,9,10

What’s going to happen when we run this script? This is what’s going to happen:


In other words, Windows PowerShell lets you access all the values in an array simply by echoing back the array itself (in this case, $x). You don’t have to set up a For Each loop or a For Next loop; PowerShell takes care of all that for you.

Of course, with VBScript you don’t have to access all the items in an array; you can also access individual items by specifying the item index number. For example, suppose you want to access the third item in the following array:

x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Because arrays in VBScript (and in PowerShell) are “0 indexed” that means that the very first item has an index number of 0, the second item has an index number of 1, and – by extension – the third item has an index number of 2. How can we access the third item and only the third item? By using code like this:

Wscript.Echo x(2)

As you might expect, Windows PowerShell enables you to do the same thing, and with very similar syntax; for example, this code echoes back the value of item 3 in a PowerShell array:


But that’s not the half of it. Want to echo back the last item in the array? That’s possible to do in VBScript, albeit by using some crazy-looking construction similar to this:

Wscript.Echo x(Ubound(x))

Here’s how you accomplish the same task in Windows PowerShell:


What’s that? What about the second-to-the-last item in the array? No problem:



But wait, there’s more. Suppose you’d like to echo back the value of items having the index numbers 1, 3, 5, and 7? Believe it or not, that’s no problem; just make sure you specify each of those index numbers, separating the individual numbers using commas:


We’ve saved the very best for the very last. Windows PowerShell features a “numeric range” operator (..) that enables you to specify a range of numbers, something that can be very useful when dealing with arrays. Suppose we have an array with 100 items in it, and we need to echo back the value of items 37-79. If we want to, we can list each of those index numbers. Or, we can use the numeric range operator instead:


Nice, huh? To add a little icing to the cake, the range operator isn’t restricted solely to arrays, either. For example, try this little one-line script and see what happens:


Even simple people with simple tastes can’t help but be impressed by that.