Comparison Operators

Comparison Operators are used to perform comparison of multiple values and finding values that match patterns. Comparison operators also have different categories e.g. Equality operators, Matching operators, Containment operators, Replacement operators and Type operators. Let's go through all operators one by one.

Equality Operators

These operators can be used to compare values, -eq, -ne, -gt, -lt, -le, -ge are comparison operators.

Example
Let's take an example, $v1 = 10 and $v2 = 12
  • eq: This operator checks both values are equal, $v1 -eq $v2 will return false
  • ne: This operator checks both valuse are not equal, $v1 -ne $v2 will return true
  • gt: This operator checks one value is greater than another value or not, $v2 -ge $v1 will return true
  • ge: This operator checks one value is greater than or equal or not, $v2 -ge $v1 will return true
  • lt: This operator checks one value is less than or not, $v1 -lt $v2 will return true
  • le: This operator checks one value is less than or equal or not, $v2 -le $v1 will return false

Matching Operators

These operators can be used to match values using wildcard characters or regular expressions, -match, -notmatch, -replace, -like, -notlike are comparison operators.

Let's look at example below...

  • -match - if single value then it returns true if matched false otherwise, if it is a list it returns matched value(s)
  • -notmatch - if single value then it returns true if matched and false otherwise, if it is a list it returns unmatched value(s)
  • -like - if single value then it returns true if matched wildcard patterns and false otherwise, if it is a list it returns wildcard pattern matched value(s)
  • -notlike - if single value then it returns true if unmatched wildcard patterns and false otherwise, if it is a list it returns wildcard pattern unmatched value(s)
Here is a sample code which will make you more clear
Example
$weekdays = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";

$day = "Wednesday";

$day -notlike "*dne*";	# This will return False
$weekdays -notlike "*tu*"	# This will return all day names except Tuesday and Saturday

$day -like "*dne*";	# This will return True
$weekdays -like "*tu*";	# This will return Tuesday and Saturday

$day -match "dne";	# This will return True
$weekdays -match "tu";	# This will return Tuesday and Saturday

$day -notmatch "dne";	# This will return False
$weekdays -notmatch "tu";	# This will return all day names except Tuesday and Saturday

Containment Operators

These operators can be used to check one value contains another value or not, -contains, -notcontains, -in, -notin are containment operators.

Let's look at example below...

  • -in - It will return true if value on left side is in the collection specified on right side.
  • -notin - It will return true if value on left side is not in the collection specified on right side.
  • -contains - It will return true if specified collection on left side contains value specified on right side.
  • -notcontains - It will return false if specified collection on left side doesn't contain value specified on right side.
Here is a sample code which will make you more clear
Example
$weekDays = "Monday", "Tuesday", "Wednesday", "Thursday", "Friday";

"Wednesday" -in $weekdays;	# This will return True
"Saturday" -in $weekdays;	# This will return False

"Wednesday" -notin $weekdays;	# This will return False
"Saturday" -notin $weekdays;	# This will return True

$weekdays -contains "Wednesday";	# This will return True
$weekdays -contains "Saturday";	# This will return False

$weekdays -notcontains "Wednesday";	# This will return False
$weekdays -notcontains "Saturday";	# This will return True

Replacement Operators

This operator can be used to replace all occurance of a string with specified string using a regular expression, -replace is a replacement operators.

Let's look at example below...

Example
$weekEnds = "Weekends start from Friday";

$weekEnds -replace "Friday", "Saturday";  #This will return "Weekends start from Saturday";

Type Comparison Operators

This operator can be used to check object is of a specific type or not. -is, -isnot are type comparison operators.

Let's look at example below...

Example
$a = 10;
$b = "10";

$a -is [string];	#This will return False, because type of $a is integer.
$b -is [string];	#This will return False, because type of $b is string.

$a -isnot $b.GetType();	#This will return True, because type of $a and $be are not same.

Special Note: Windows PowerShell's operators do comparison case-insensitive by default. But if you want to do case-sensitive comparison then you can add a 'c' after '-' to all operators e.g. -like to -clike, -eq to -ceq, -match to -cmatch. And for case-insensitive you can add 'i' after - to all operators e.g. -like to -ilike, -eq to -ieq, -match to -imatch.

 

← Previous

Next →