PowerShell: Test Equality

By Xah Lee. Date: . Last updated: .

💡 TIP: Operator names are case-insensitive.

Equality Operator: -eq

value1 -eq value2

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

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

filter collection, filter by value. Case-Insensitive.

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

# 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
# empty array

$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 operator (Test not equal)

-ne

negation of -eq

-ine

negation of -ieq

"a" -ine "A"
# False
-cne

negation of -ceq

"a" -cne "A"
# True

PowerShell, Boolean Related