共用方式為


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.

Removing Items From Arrays

There’s no doubt that arrays are incredibly useful when writing system administration scripts. There’s even less doubt (assuming you can have less doubt than no doubt) that Windows PowerShell makes it very easy to work with arrays; to that end, we’ve talked about different tricks you can do with arrays in several of our previous Tips of the Week. Nevertheless, the array class built into Windows PowerShell does have at least one weakness: as easy as it might be to add a new item to an array, there’s no comparably-easy way to remove an existing item from an array. That’s a shame, but, then again, that’s just the way it goes. After all, you have to use the array class built right into Windows PowerShell, don’t you?

Using the .NET Framework ArrayList Class

You already figured out the answer to our question, didn’t you? Do you have to use the array class built into Windows PowerShell? Of course not. After all, Windows PowerShell provides complete access to the .NET Framework, and the .NET Framework offers all sorts of alternative arrays, collections, and hash tables. Don’t like the array class built into Windows PowerShell? Then just use a different array type.

Good point: which different array type are we supposed to use? Well, one array type that’s worth investigating is the System.Collections.ArrayList class. To use this array type, all we have to do is use the New-Object cmdlet to create a new instance of the ArrayList class, an instance that we’ve named $a:

$a = New-Object System.Collections.ArrayList

That command gives us an empty array named $a. If we then want to populate that array with some information (which we probably do), all we have to do is call the Add method followed by the item we want to add to the array. For example, here is a series of commands that adds six different color names to our array:

$a.Add("red") 
$a.Add("yellow") 
$a.Add("orange") 
$a.Add("green") 
$a.Add("blue") 
$a.Add("purple")

If we now echo the value of $a, we’ll get back the following:

red
yellow
orange
green
blue
purple

Pretty cool, huh? Oh; we see. You don’t seem to be very impressed; maybe that’s because, so far anyway, we haven’t really done much. But don’t worry; that’s about to change.

To begin with, let’s see if we can remove a specified item from our array. With the standard Windows PowerShell array class that’s a difficult proposition, at best; as the Windows PowerShell help documentation states:

It is not easy to delete elements from an array, but you can create a new array that contains only selected elements of an existing array. For example, to create the $t array with all elements in the $a array, except for the value at index position 2, type:

$t = $a[0,1 + 3..($a.length - 1)]

Admittedly, that works. On the other hand, it’s not particularly intuitive, and relies on you knowing the index number of the item you want to remove; you can’t simply say, “Hey, $a: can you remove the color yellow for me?” And yet, that’s pretty much all you need to do when working with the ArrayList class:

$a.Remove("yellow")

As you can see, that’s about as simple a command as you’ll ever use: all you do is call the Remove method, tacking on the item to be removed as the sole method parameter. Now take a look at the value of $a; notice anything missing?

red
orange
green
blue
purple

That’s right: yellow has been removed, and without us doing anything more complicated that calling the Remove method. Now that is cool.

Here’s another way to quickly and easily remove items (yes, we said items) from an array. Suppose we have our original array, the one with the six different colors in it. For some reason, we’ve decided to keep the first three colors (red, yellow, and orange) and toss out the last three colors. How do we do that? Like this:

$a.RemoveRange(3,3)

What we’ve done here is call the RemoveRange method, a method that enables us to delete a range of items from an ArrayList. Notice that we passed RemoveRange a pair of parameters, in this case a pair of 3s. The first 3 represents the index number of the first item to be removed. Like most arrays, the first item in an ArrayList has an index number of 0; that means that the second item has an index number of 1, and the third item has an index number of 2. Does that matter to us? You bet it does; after all, that means the fourth item (the first item to be removed) has an index number of 3. Hence the first 3, which tells RemoveRange that the first item we want to delete is the item with the index number 3 (the fourth item in the array).

So then what’s the second 3 for? That’s easy: that’s number of items we want to remove. Suppose we only want to remove items 4 and 5, leaving item 6. In that case, we’d use this command:

$a.RemoveRange(3,2)

That would make $a equal to this:

red
orange
yellow
purple

Incidentally, you can determine the number of items in an array simply by echoing back the value of the Count property, like so:

$a.Count

Oh, one more thing: what if you to get rid of all the items in the array? Here’s one thought; call the Clear method:

$a.Clear()

And that’s just one of the many alternative arrays, collections, and hash tables available in the .NET Framework. Next week we’ll talk about another such alternative: the hash table.