PowerShell: Comparison Operators

By Xah Lee. Date: . Last updated: .

Operator names are case-insensitive.

String Operators

PowerShell: String Operators


check equality or filter array. Case-Insensitive.
Same as -eq
Case-sensitive version of -eq
negation of -eq
negation of -ieq
negation of -ceq

-eq is Not Reflexive (Order Matters)

PowerShell comparison operators are not reflexive. That is, order matters. x ▲ y and y ▲ x may return different results, if x and y are not both numbers or string.

For example:

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

$x -eq $y
# return 3

$y -eq $x
# return False

Using -eq to Test Equality

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

"a" -ceq "A"
# False
"a" -ine "A"
# False

"a" -cne "A"
# True

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

Using -eq to Filter Array

If the left side of -eq is array, it'll return a array with elements that compares True.

# using -eq as array filter
3,4,5 -eq 3
# return 3

3,4,5 -eq 6
# return empty array

"cat", "dog", "bird" -eq "dog"
# return "dog"

# the array must be on the left side
3 -eq 3,4,5
# return False


greater than
greater than or equal
less than
less than or equal
3 -lt 4 #True
4 -lt 3 #False
case-insensitive -gt
case-insensitive -ge
case-insensitive -lt
case-insensitive -le
case-sensitive -gt
case-sensitive -ge
case-sensitive -lt
case-sensitive -le

Collection Contains

collection contains a value. case-insensitive.
same as -contains.
case-sensitive version of -contains.
$x = 1,2,3
$x -contains 3
# True
$x = "A","B"
$x -contains "a"
# True

$x = "A","B"
$x -icontains "a"
# True

$x = "A","B"
$x -ccontains "a"
# False
negation of -contains
same as -notcontains
case-sensitive version of -notcontains
$x = "A","B"
$x -notcontains "a"
# False

$x = "A","B"
$x -inotcontains "a"
# False

$x = "A","B"
$x -cnotcontains "a"
# True
value is in a collection. The -in operator is like -contains, but the operands are in reverse order.
value is not in a collection
$x = 1,2,3
3 -in $x
# True
case-insensitive, same as -in
case-insensitive version of -notin
case-sensitive, same as -in
case-sensitive version of -notin

Check Type

both objects are the same type
the objects are not the same type

Case-Sensitive Comparison Operators

For each comparison operator, there's a corresponding one that's case-sensitive. The operator's name start with c. For example -eq vs -ceq. Also, you can prefix it by i (for insensitive), such as -ieq. It is same as -eq.




Script Examples