PowerShell: Comparison Operators

By Xah Lee. Date: . Last updated: .

Operator names are case-insensitive.

String Operators

PowerShell: String Operators

Equality

-eq
check equality or filter array. Case-Insensitive.
-ieq
Same as -eq
-ceq
Case-sensitive version of -eq
-ne
negation of -eq
-ine
negation of -ieq
-cne
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
#True
"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/Lesser

-gt
greater than
-ge
greater than or equal
-lt
less than
-le
less than or equal
3 -lt 4 #True
4 -lt 3 #False
-igt
case-insensitive -gt
-ige
case-insensitive -ge
-ilt
case-insensitive -lt
-ile
case-insensitive -le
-cgt
case-sensitive -gt
-cge
case-sensitive -ge
-clt
case-sensitive -lt
-cle
case-sensitive -le

Collection Contains

-contains
collection contains a value. case-insensitive.
-icontains
same as -contains.
-ccontains
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
-notcontains
negation of -contains
-inotcontains
same as -notcontains
-cnotcontains
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
-in
value is in a collection. The -in operator is like -contains, but the operands are in reverse order.
-notin
value is not in a collection
$x = 1,2,3
3 -in $x
# True
-iin
case-insensitive, same as -in
-inotin
case-insensitive version of -notin
-cin
case-sensitive, same as -in
-cnotin
case-sensitive version of -notin

Check Type

-is
both objects are the same type
-isnot
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.

PowerShell

How-to

Advanced

Script Examples