Export (0) Print
Expand All
Expand Minimize

Windows PowerShell Tip of the Week

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.

Even More Things You Can Do With Arrays

One of the very first things people notice about Windows PowerShell is this: PowerShell makes it very easy to create arrays and to add new items to arrays. With VBScript, these same tasks are a bit … challenging … to say the least: you either have to declare the array size in advance (leaving you with an array that can’t be resized), or you have to declare a dynamic array, then resize that array each time you add a new item. (Taking care, of course, to preserve all the existing items in the array.) Sure that sounds like a lot of fun. But, trust us: it’s not

With Windows PowerShell, however, things are much different, and much easier. Want to create an array named $a, an array that contains the values 1, 2, and 3? Okey-doke:



$a = 1, 2, 3



That’s all you have to do: just separate the items using commas and add them all to the array in one fell swoop. Can you create an array of string values using this same approach? Of course you can; just make sure you enclose the string values in double quote marks:



$a = "one", "two", "three"



Ah, good question: what if you do want to add a new item to the array? No problem; this line of code will do the trick:



$a = $a + 4



If $a formerly contained the values 1, 2, and 3, what do you suppose $a will be equal to now? You’re absolutely right:



1
2
3
4



So is this the easiest thing you’ll ever do in your entire life? Probably; we can’t think of too may things that are easier.

Of course, because creating new arrays and adding items to existing arrays is so easy you might think you know everything there is to know about arrays in Windows PowerShell. Is that really true? Well, maybe; let’s find out for ourselves.

Adding a Range of Values to an Array

Here’s a cool little trick. Suppose you need an array containing the numbers 1 through 100. If you want to, you can set up a For loop that loops through that set of numbers, methodically adding each value to the array, one-by-one. Alternatively, you can use PowerShell’s range operator, like so:



$a = 1..100



The range operator (..) simply tells Windows PowerShell to take the first value (1), the last value (100), and all the other values in between. One line of code, and you’ve created an array containing all the whole numbers between 1 and 100.

See? We told you that this was a cool little trick.

Creating an Empty Array

There will often be times when you need to create an array even though you don’t actually have any values (at least not at the moment) to assign to that array. In VBScript you might use code similar to this to create an empty array:



Dim A(10)



That gives you an array that can, in theory, contain as many as 11 items. At the moment, however, this is an empty array; it doesn’t contain any items. And that’s fine; at the moment we don’t want it to contain any items.

It’s pretty easy to create an empty array in Windows PowerShell; the one catch is that the method for doing so isn’t the most intuitive thing we’ve ever seen. For example, you might think that code similar to this will do the trick:



$a
$a = 1
$a = $a + 2



It won’t. When you add 1 to the variable $a, $a becomes a variant with a value equal to, well, 1. So then what happens if you add 2 to $a? That’s right; you don’t get an array of two items (1 and 2). Instead, you get the value of 1 plus 2:



3



Not exactly what we had in mind.

And, before you ask, this approach is equally-doomed to fail:



[array] $a
$a = 1
$a = $a + 2



You’ll get the same answer here as you did before: 3.

Instead, this is how you create an empty array in Windows PowerShell:



$a = @()



Like we said, the syntax isn’t the most intuitive thing in the world. Nevertheless, it works. To verify this for yourself, try running the following block of code:



$a = @()
$a = $a + 1
$a = $a + 2



Here’s what you should see when you echo back the value of $a:



1
2



Much better.

Creating a Strongly-Typed Array

By default, a new array created in Windows PowerShell is an array of variants: that means you can put any kind of data (string values, numeric values, date-time values) into that array. In addition, you can mix data types in the same array; this is a perfectly valid line of Windows PowerShell code:



$a = 1, "A", (Get-Date)



That’s going to give us an array with values like this:



1
A
Friday, August 03, 2007 9:00:08 AM



Most of the time this is exactly what you want; after all, it enables you to create an array without having to worry about the type of data that can or cannot be added to that array. At other times, however, you might want to create a “strongly-typed” array, an array that can contain only numbers or only dates or only whatever. How do you do that in Windows PowerShell? Here’s one way:



[int[]] $a = 1,2,3,4



As you can see, all we did here was specify the integer data type (int) before declaring the variable $a and assigning that variable a set of values. (By the way, note the additional set of square brackets – [ ] – that follows the data type name.) Granted, this doesn’t look like much, and you won’t notice any difference when echoing back the value of the array; request the value of $a and you’ll get back this:



1
2
3
4



So what’s the big deal? Well, try adding a new item to the array, one that isn’t an integer. For example:



$a = $a + "test"



Here’s what’s going to happen:



Cannot convert value "test" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:3

+ $a  <<<< = $a + "test"



That line of code is going to fail, because you can’t add a string value to an array of integers. This line of code will also fail:



$a = $a + (Get-Date)



And for the same reason: a date isn’t an integer value, so you can’t add a date to an array of integers. Etc.

The one thing to be aware of here is that non-integer numbers can lead to unexpected results. For example, take this line of code:



$a = $a + 5.12345



Needless to say, 5.12345 isn’t an integer. However, you won’t get an error when you execute this command; instead, Windows PowerShell will strip off the decimal places and add just the integer value (5) to the array. By all rights, you might expect $a to equal this:



1
2
3
4
5.12345



In reality, however, it’s going to equal this:



1
2
3
4
5



Something to keep in mind.

Combining Two or More Arrays

One last tip before we call it a day. Suppose you have two arrays. The array $a contains these values:



Red
White
Blue



Meanwhile, the array $b contains these values:



Green
Orange
Yellow



You’d like to combine these two arrays into a single array ($c). How can you do that? Simply by adding the two arrays together:



$c = $a + $b



Give that a try and see what happens.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft