Thursday, January 30, 2014

PowerShell - Less common use of -eq to find matches

Yes, its hard to believe I would have anything to say about "-eq" as it is such a basic thing to know about. However their is one usage that is less common and worthy of note. Normally we are used to the format:

something -eq something

Which would return TRUE or FALSE depending on whether the two values are the same (not withstanding the fact that comparing strings are non-casesensitive by default (see "-ceq"). However "-eq" can be used to look for matches with arrays. Try running this:

"apple", "banana", "orange" -eq "fred"
"apple", "banana", "orange" -eq "orange"

$array1 = "apple", "banana", "orange"
$array1 -eq "fred"
$array1 -eq "orange"

You will see that whether you assign to an array variable or not, the "-eq" can be used to find matches. In many ways the more significant thing is that the expression does not return a boolean - it returns either the value of the match or a NULL

No comments:

Post a Comment