Left Side of Comparison

by Jun 19, 2019

When using comparison operators, always make sure the relevant part is placed left. That’s because PowerShell looks at the left side of an operator and may automatically change types of the right side. Also, comparison operators may work as filters when there is an array on the left side.

Check out the differences:

$array = @()

'-'* 80
$array -eq $null
'-'* 80
$null -eq $array
'-'* 80

Since you are comparing an array, the comparison operator works like a filter and returns nothing in the first comparison. When you swap operands, it returns $false which is the right answer: an array, even an empty array, is not null.

Twitter This Tip! ReTweet this Tip!