What Is (and What Isn’t) in Our Array?

 

 

Thus far, several of our weekly Windows PowerShell Tips have dealt with arrays. And there’s a good reason for that: arrays are one area where Windows PowerShell really distinguishes itself. It’s debatable whether PowerShell’s Forloops or If statements are any better (or worse) than VBScript’s or JScript’s. But there’s no question that PowerShell has a leg up on both of these scripting languages when it comes to working with arrays.

 

For example, take the following VBScript command, one that assigns a number of color values to the array arrColors:

 

arrColors = Array("blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise")

 

Now, consider this seemingly-simple question: does the color blackappear anywhere within the array arrColors?

 

Like we said, that seemslike a simple enough question. In VBScript, however, answering that question is anything butsimple; as it is, you have to set up a For Each loop, loop through and check each individual item in the array, keep track of whether or not you encounter the word black, and thenreport back the answer. That means that your script will have to look something like this:

 

blnFound = False

arrColors = Array("blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise")

For Each strColorin arrColors
    If strColor= "black" Then
        blnFound= True
    End If
Next

Wscript.Echo blnFound

 

Which results in output like this:

 

0

 

And yes, VBScript echoes back a 0 if the value is False and a -1 if the value is True. If you’d rather see the words Trueor False, well, you’ll have to make even moremodifications to the script.

 

So then how do we do this same thing in Windows PowerShell? Like this:

 

$ arrColors= "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
$ arrColors–contains "black"

 

Believe it or not, that’s all there is to it: all we have to do is use the –containsoperator, followed by the value we’re searching for. As you might have guessed, the –contains operator tells us whether or not an item can be found in a group. Because the color blackdoes notappear in our array, PowerShell is going to respond by telling us this:

 

False

 

Pretty handy.(And notice that, as an added bonus, we got back the word Falserather than a 0.)

 

Now, what if you’d like to know if the array $ arrColors doesn’tcontain the color purple. In that case, just use the notcontainsoperator, an operator that returns Trueif the target value can’tbe found in the array. In other words:

 

$ arrColors- notcontains"violet"

 

Here’s what we get back:

 

True

 

Why? Because it’s true that the word violetis not an item in our array.

 

This is probably a good time to emphasize that we are looking at complete array items. Suppose we added Violet Beauregard(from the movie Willie Wonkaand the Chocolate Factory) to our array. What do you think we’ll get back if we run this command:

 

$ arrColors-contains "violet"

 

That’s right; we’ll get back False. That’s because there’s no array item named violet. There isan item named Violet Beauregard, but that’s obviously not the same item.

 

A (Case-) Sensitive Subject

 

Like most (if not all) Windows PowerShell operators, you can also perform case-sensitive checks using the ccontainsand the cnotcontainsoperators (the letter ctacked onto the front of each operator stands for “case sensitive”). For example:

 

$ arrColorsccontains"Green"

 

This command returns False, because – in a case-sensitive comparison – the value Greenis not the same as the value green.

 

Like, Do We Have Any of These or Not?

 

Here’s another nifty trick for quickly checking to see if any values exist in an array. Suppose we add the color blackto our array:

 

$ arrColors+= "black"

 

That means $ arrColorsnow contains the following elements:

 

blue
red
green
yellow
white
pink
orange
turquoise
black

 

That’s nice. Now, how can we query $ arrColorsto determine whether or not any of the values in the array begin with the letters bl ? Here’s how:

 

$ arrColors–like " bl*"

 

What we’re doing here is using the –likeoperator and the wildcard character (the asterisk) to check for the existence of any values in $ arrColorsthat begin with the letters bl . Here’s how PowerShell responds (note that, with –like, you get back the actual values rather than a Boolean True or False):

 

blue
black

 

Very nice.If you’d like to create a new array ($ arrSubset) containing those values, well, that only requires one line of code as well:

 

$ arrSubset= $ arrColors–like " bl*"

 

Like we said: very nice.

  

A Place for Everything, and Everything in Its Place

 

Of course, we still have one minor problem: the items in our array aren’t in alphabetical order. Sorting an array in VBScript is an… interesting … experience, to say the least. Here’s how you can sort an array in Windows PowerShell:

 

$ arrColors= $ arrColors| Sort-Object

 

You can see what’s going on here: we’re taking our array ($ arrColors) and “piping” it to the Sort-Objectcmdlet. After Sort-Object sorts the items in the array, we then assign this new, sorted list back to $ arrColors. So nowwhat is $ arrColorsequal to? This:

 

black
blue
green
orange
pink
red
turquoise
white
yellow

 

Which is just exactly what we wantedit to be equal to.