PowerShell: Test Equality

By Xah Lee. Date: . Last updated: .

💡 TIP: Operator names are case-insensitive.

🛑 WARNING: The -eq operator is both for testing equality and for filtering array. For testing equality, make sure the left-hand-side is a scalar value.

-eq (equality operator)

value1 -eq value2

return $true if value1 equals to value2. Case-Insensitive.

🛑 WARNING: value1 must not be a collection type.

3 -eq 3
# True
"a" -eq "A"
# True

-eq as array filter

collection -eq value

filter collection, filter by value. Case-Insensitive.

# using -eq as array filter
$bb = 3,4,5,3 -eq 3
Write-Host $bb
# 3 3

$cc = 3,4,5 -eq 6
Write-Host ($cc.length -eq 0)
# True

$dd = "cat", "dog", "bird" -eq "dog"
Write-Host $dd
# dog
# the array must be on the left side
3 -eq 3,4,5
# False
-ieq

Same as -eq

-ceq

Case-sensitive version of -eq

"a" -ceq "A"
# False

🛑 WARNING: -eq is Not Symmetric. (Order Matters)

PowerShell equality operator is not symmetric (nor Reflexive). That is, order matters. x -eq y and y -eq x may return different results, if x and y are not both numbers or string.

# the -eq operator is not symmetric

$x = 3,4,5
$y = 3

$x -eq $y
# return 3

$y -eq $x
# return False

Test for $null

To test for $null, always put $null on left-hand-side.

$x = 1,2,3

$null -eq $x
# return False

$x -eq $null
# return empty array

-ne (not equal operator)

-ne

negation of -eq

-ine

negation of -ieq

-cne

negation of -ceq

PowerShell. Boolean Related