My Powershell Surprise Of The Day: An Array’s -eq Operator

I’m always discovering stuff in Powershell.  I know I’m not a guru but I don’t think of myself as a novice either—and yet I keep coming across something that makes me think, “how could I have missed something so basic?”  Today it was the –eq operator for an array.

Studying a great introductory article on WPK, I was puzzled about something the author apparently thought everyone already knew: $myArray –eq $myValue will return $myValue (more correctly, an array containing $myValue) if it’s contained in $myArray; i.e., if $myArray –contains $myValue is true.

Not knowing this, I’ve written code that uses the –contains operator, and if it evaluates to true, I would pass $myValue on.  Something like this:  if ($myArray –contains $myValue) {$myValue}.  I think that I could argue that this code is clearer in its intent than using the –eq operator.  But the latter sure reduces the volume of code.  It looks quite elegant if you know the secret.

Ever ready to discover more of my ignorance, I decided to play with this a bit more to learn how –eq and –contains work on arrays.  As I expected, simple data types worked as described above.  As expected, something as complex as a hashtable did not result in this behavior—the comparison doesn’t go deep enough into the objects to determine equality.  But if you use two variables to refer to the same actual object, –eq and –contains work as I described.  This is all pretty standard stuff.  But I wasn’t sure about a TimeSpan, which is of base type System.ValueType—same as an int.  And yes, –eq and –contains work the same as for an int.

Here’s the test code I used to learn about it.  Just quick and dirty stuff but I learn things when I goof around.  And I guess that’s the point here—learn from others, then do some playing to extend what you’ve heard.


" ------------ strings --------------"
$arrayOfStrings = "This", "is", "a", "test"
$inStringArray = "is"
$notInStringArray = "no"

"check inStringArray"
$arrayOfStrings -eq $inStringArray
$arrayOfStrings -contains $inStringArray

"`r`ncheck notinStringArray"
$arrayOfStrings -eq $notInStringArray
$arrayOfStrings -contains $notInStringArray

" --------------- ints ---------------"
$arrayOfInts = 1, 2, 3, 4
$inIntArray = 4
$notInIntArray = 5

"`r`ncheck inIntArray"
$intResult = $arrayOfInts -eq $inIntArray
$arrayOfInts -contains $inIntArray
$intResult.getType() | select name

"`r`ncheck notInIntArray"
$notIntResult = $arrayOfInts -eq $notInIntArray
$arrayOfInts -contains $notInIntArray
$notIntResult.getType() | select name

" ---------------- TimeSpan -----------"
$timeSpanArray = [TimeSpan] "0:1:15", [TimeSpan] "0:1:20", [TimeSpan] "0:1:25"
$inTimeSpanArray = [TimeSpan] "0:1:20"

"`r`ncheck inTimeSpan"
$tsResult = $timeSpanArray -eq $inTimeSpanArray
$timeSpanArray -contains $inTimeSpanArray

" --------------- HashTables -----------"
$hashArray = @{firstName="George"; lastName="Harrison"}, @{firstName="Paul"; lastName="McCartney"}, 
        @{firstName="Ringo"; lastName="Starr"}, @{firstName="John"; lastName="Lennon"}
$inHash = @{firstName="Paul"; lastName="McCartney"}
$inHashByRef = $hashArray[1]

"`r`ncheck inHash"
$hashArray -eq $inHash
$hashArray -contains $inHash

"`r`ncheck inHashByRef"
$result = $hashArray -eq $inHashByRef
$hashArray -contains $inHashByRef
$result.getType() | select name
$result[0].getType() | select name


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: