What are PowerShell comparison operators?

PowerShell comparison operators are used to compare values. There are four different categories of comparison operators in PowerShell. They are as follows:

  1. Equality operators
  2. Matching operators
  3. Replacement operators
  4. Containment operators
  5. Type checking operators

Equality operators

The equality operators are used to check whether two values are equal or not, lesser or greater than one another.

Operator Description
-eq It checks whether two values are equal
-ne It checks whether two values are not equal
-gt It checks whether the left hand side is greater than the right hand side
-lt It checks whether the left hand side is lesser than the right hand side
-ge It checks whether the left hand side is greater than or equal to the right hand side
-le It checks whether the left hand side is lesser than or equal to the right hand side

Syntax

<left hand side> <operator> <right hand side>

Comparison can be case-sensitive or case-insensitive in nature. Hence, to perform a case-sensitive comparison, prefix the above operators with c, such as -ceq, -cne, -clt, -cgt etc.

Similarly, to perform a case-insensitive comparison prefix the above operators with i i.e -ieq, -ine, -ilt, -igt etc.

Code example

The following code is executed in the terminal below:

$a=10  
$b=5
($a -eq $b)
($a -gt $b)
($b -le $a)

Here, we define two variables, a and b, and apply different equality operators.

Terminal 1
Terminal
Loading...

Matching operators

The matching operators are used to check whether the value matches the specified pattern or not.

Operator Description
-like It checks whether the string matches the wildcard pattern
-notlike It checks whether the string does not match the wildcard pattern
-match It checks whether the string matches the regular expression
-notmatch It checks whether the string does not match the regular expression

Syntax

<string[]> [-like | -notlike]  <wildcard-expression>
<string[]> [-match | -notmatch] <regular-expression>

Matching can be case-sensitive or case-insensitive in nature. Hence, to perform a case-sensitive matching prefix, use the above operators with c such as -clike, -cnotlike, -cmatch, -cnotmatch.

Similarly, to perform a case-insensitive matching prefix, use the above operators with i, such as -ilike, -inotlike, -imatch, -inotmatch.

Code example

The following code is executed in the following terminal:

pwsh
$a="Educative"  
$b="edu*"  
$a -ilike $b
$b="Edu*"  
$a -like $b
$a -notlike $b
$a="Monday", "Tuesday", "Wednesday"
$b="Wed"  
$a -match $b  

Here, we define two variables, a and b, and apply different matching operators.

Terminal 1
Terminal
Loading...

Replacement operators

The replacement operators are used to replace the string matching the given regex with a replacement value.

Operator Description
-replace It checks whether the string matches the wildcard pattern

Syntax

<input> -replace <regular-expression>, <substitute>

Replacement can be case-sensitive or case-insensitive in nature. Hence, to perform a case-sensitive replacement, prefix the above operators with c such as -creplace.

Similarly, to perform a case-insensitive replacement prefix the above operators with i, such as -ireplace.

Code example

The following code is executed in the below terminal.

$a="Educative and edpresso"  
$a -replace "edpresso" , "Answers"   

Here, we define two variables, a and b, and apply different replacement operators.

Terminal 1
Terminal
Loading...

Containment operators

The containment operators are used to check whether a collection contains a value or not.

Operator Description
-contains It checks whether the collection contains the value
-notcontains It checks whether the collection does contain the value
-in It checks whether a value is contained in the collection
-notin It checks whether a value is not contained in the collection

Syntax

<Collection> [-contains | -notcontains] <Test-object>
<Test-object> [-in | -notin] <Collection>

Containment can be case-sensitive or case-insensitive in nature. Hence, to perform a case-sensitive containment check prefix the above operators with c, such as -ccontains, -cnotcontains,-cin, cnotin.

Similarly, to perform a case-insensitive contains prefix the above operators with i, such as -icontains, -inotcontains,-iin, inotin.

Code example

The following code is executed in the below terminal.

$a="Monday", "Tuesday", "Wednesday"
$b="Wednesday"  
$a -contains $b
$a -notcontains $b
$b -in $a
$b -notin $a

Here, we define two variables, a and b, and apply different containment operators.

Terminal 1
Terminal
Loading...

Type checking operators

The type checking operators are used to check whether both the given objects are of the same type or not.

Operator Description
-is It checks whether both the objects are of the same type
-isnot It checks whether both the objects are not of the same type

Syntax

<object> -is <type-reference>
<object> -isnot <type-reference>

Code example

The following code is executed in the following terminal:

$a="hello"
$a -is [Int]
$a -isnot [Int]
$a -is [string]

Here, we define a variables a and apply different type check operators.

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved