PowerShell: Test Equality

By Xah Lee. Date: . Last updated: .

Operator names are case-insensitive.

-eq operator

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, return items that is equal to value. Case-Insensitive.

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
-ieq
Same as -eq
-ceq
Case-sensitive version of -eq
"a" -ceq "A"
# False

-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

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 Operators

PowerShell

Practical How-To

list files

list dirs

on dir

on file

Windows config

zip tar

misc


PowerShell in Depth

Programing PowerShell

Value Types

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Input/Output

Function

Profile and Script

Script Examples