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.

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 For loops 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 black appear anywhere within the array arrColors?

Like we said, that seems like a simple enough question. In VBScript, however, answering that question is anything but simple; 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 then report 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 strColor in 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 True or False, well, you’ll have to make even more modifications 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 –contains operator, 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 black does not appear 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 False rather than a 0.)

Now, what if you’d like to know if the array $arrColors doesn’t contain the color purple. In that case, just use the –notcontains operator, an operator that returns True if the target value can’t be 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 violet is 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 Wonka and 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 is an 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 –ccontains and the –cnotcontains operators (the letter c tacked onto the front of each operator stands for “case sensitive”). For example:



$arrColors -ccontains "Green"



This command returns False, because – in a case-sensitive comparison – the value Green is 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 black to our array:



$arrColors += "black"



That means $arrColors now contains the following elements:



blue
red
green
yellow
white
pink
orange
turquoise
black



That’s nice. Now, how can we query $arrColors to 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 –like operator and the wildcard character (the asterisk) to check for the existence of any values in $arrColors that 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-Object cmdlet. After Sort-Object sorts the items in the array, we then assign this new, sorted list back to $arrColors. So now what is $arrColors equal to? This:



black
blue
green
orange
pink
red
turquoise
white
yellow



Which is just exactly what we wanted it to be equal to.

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