Comparison Operators and Conditional Logic

Comparison operators let you specify conditions for comparing values and finding values that match specified patterns. To use a comparison operator, specify the values that you want to compare together with an operator that separates these values.

Equality

Operatorsย Description
-eqequals
-nenot equals
-gtgreater than
-gegreater than or equal
-ltless than
-leless than or equal



Containmentย 

OperatorsDescription
-containsReturns true when reference value contained in a collection
-notcontainsReturns true when reference value not contained in a collection
-inReturns true when test value contained in a collection
-notinReturns true when test value not contained in a collection

Matching

OperatorsDescription
-likeReturns true when string matches wildcard pattern
-notlikeReturns true when string does not match wildcard pattern
-matchReturns true when string matches regex pattern – $matches contains matching strings
-notmatchReturns true when string does not match regex pattern – $matches contains matching strings

Replacement

Operatorsย Description
-replacereplace a string pattern

Type comparison

Operatorsย Description
-isReturns true if both object are the same type
-isnotReturns true if the objects are not the same type

By default, all comparison operators in powershell’s are case-insensitive. If we need to make a comparison operator case-sensitive, precede the operator name with a “c”. For example, the case-sensitive version of -eq is -ceq. If we need to the case-insensitivity explicit, precede the operator with an i. For example, the explicitly case-insensitive version of -eq is -ieq. Let we discuss more about the each set of operators in upcoming articles.