Comparison Operators

The PowerShell comparison operators allow you to compare expressions against each other. By default, PowerShell’s comparison operators are case insensitive. For all operators where case sensitivity applies, the –i prefix makes this case insensitivity explicit, while the –c prefix performs a case-sensitive comparison.

Windows PowerShell comparison operators

Operator

Meaning

-eq

The equality operator:

$leftValue –eq $rightValue

For all primitive types, returns $true if $leftValue and $rightValue are equal.

When used with arrays, returns all elements in $leftValue that are equal to $rightValue.

When used with any other type, PowerShell uses that type’s .Equals() method if it implements one.

-ne

The negated equality operator:

$leftValue –ne $rightValue

For all primitive types, returns $true if $leftValue and $rightValue are not equal.

When used with arrays, returns all elements in $leftValue that are not equal to $rightValue.

When used with any other type, PowerShell returns the negation of that type’s .Equals() method if it implements one.

-ge

The greater-than-or-equal operator:

$leftValue –ge $rightValue

For all primitive types, returns $true if $leftValue is greater than or equal to $rightValue.

When used with arrays, returns all elements in $leftValue that are greater than or equal to $rightValue.

When used with any other type, PowerShell returns the result of that object’s .Compare() method if it implements one. If the method returns a number greater than or equal to ...

Get Windows PowerShell Quick Reference now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.