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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.