Powershell

How to Use Comparison Operators in PowerShell?

In PowerShell, the “Compare” operators are used to compare the provided values. It compares the strings and numbers which are stored in the variables. Typically, comparison operators are the same for all programming languages. The comparison operators help the users to test, find, replace, and modify provided values or informational data. Comparison operators include matching, equality, or containment.

This post will discuss the comparison operators in PowerShell.

Understanding the PowerShell Comparison Operators

In PowerShell, multiple types of comparison operators are used, such as:

Method 1: Matching Operators

The matching operators are utilized for comparing the string values with the aid of regular expression. Moreover, it uses the wildcard character “*” to find the exact match. Matching operators include “-like”, “-notlike”,“-match”, and “-notmatch”.

Example 1: Use the “-match” Matching Operator to Compare Two Values

This example will compare the two variable values using the “-match” parameter. If the value is found then it will display the resultant value:

$season = "Winter", "Summer", "Spring", "Autumn"
$check = "Summ"
$season -match $check

Example 2: Use the “-notmatch” Matching Operator to Compare Two Values

The “-notmatch” operator is used for displaying the values that are not matching the specified value. As follows:

$season = "Winter", "Summer", "Spring", "Autumn"
$check = "Summ"
$season -notmatch $check

Example 3: Use the “-like” Matching Operator to Compare Two Values

The “-like” operator is used to display the matching values in the given variable using the wildcard “*” operator:

$val1 = "Beautiful"
$val2 = "*ful"
$val1 -like $val2

Example 4: Use the “-notlike” Matching Operator to Compare Two Values

In this example, the “-notlike” operator will display the value “True” if the values of the variables are not the same:

$val1 = "Beautiful"
$val2 = "World"
$val1 -notlike $val2

Method 2: Equality Operators

The equality operator in PowerShell checks if the two provided values have the same values or not. Moreover, it also specifies whether one value is greater or less than the other. Equality operators include “-eq“, “-ne“, “-gt“, “-ge“, “-lt“, or “-le“.

Example 1: Use the “-eq” Equality Operator to Compare the Two Values

The “-eq” operator is used to display the Boolean value “True”, if the value of both variables is the same:

$val1 = 15
$val2 = 15
$val1 -eq $val2

Example 2: Use the “-ne” Equality Operator to Compare the Two Values

In this example, the “-ne” operator is used for displaying the value “True” only if the value of both variables is satisfied:

$val1 = 15
$val2 = 25
$val1 -ne $val2

Example 3: Use the “-gt” Equality Operator to Compare the Two Values

The “-gt” operator is utilized for displaying the “True” value if the provided condition is satisfied:

$val1 = 25
$val2 = 15
$val1 -gt $val2

Example 4: Use the “-ge” Equality Operator to Compare the Two Values

The operator “-ge” is used to check whether the provided value is equal to or greater than the other or not:

$val1 = 25
$val2 = 15
$val1 -ge $val2

Example 5: Use the “-lt” Equality Operator to Compare the Two Values

The “-lt” operator is used to check if the provided value is less than the other value or not:

$val1 = 25
$val2 = 15
$val1 -lt $val2

Example 6: Use the “-le” Equality Operator to Compare the Two Values

The “-le” operator is used to check the provided variable is less than or greater than the other:

$val1 = 25
$val2 = 15
$val1 -le $val2

Method 3: Containment Operators

The containment operator is similar to the equality operator. However, it returns the output in the Boolean value form that is either “True” or “False”. The containment operator checks whether a value on the left exists in the right-side list of values or not. If the value existed in the list, then the resultant output will be “True” else it will be “False”. The containment operators include “-contains”, “-notcontains”, “-in”, or “-notin”.

Example 1: Use the “-contains” Containment Operator to Compare the Two Values

The operator “-contains” is utilized to test if the value is available in the given set or not:

$days = "Saturday", "Sunday","Monday"
$day = "Sunday"
$days -contains $day

Example 2: Use the “-notcontains” Containment Operator to Compare the Two Values

The “-notcontains” operator is utilized to check the provided value is not available in the given set. If the condition is satisfied then the output will be “True”:

$days = "Saturday", "Sunday","Monday"
$day = "Sunday"
$days -notcontains $day

Example 3: Use the “-in” Containment Operator to Compare the Two Values

The “-in” operator is used to check whether the provided value is available in the specified set or not:

$a = "Apple"
$b = "Mango", "Apple", "Orange"
$a -in $b

Example 4: Use the “-notin” Containment Operator to Compare the Two Values

The “-notin” operator gives the “True” value only if the specific set does not contain the specified value:

$a = "Apple"
$b = "Mango", "Apple", "Orange"
$a -notin $b

That’s all! We have described the usage of the “Comparison” operators in PowerShell.

Conclusion

In PowerShell, the comparison operators are utilized to compare the values of objects or variables. It could be strings or numbers. Comparison operators include equality, matching, or containment. This blog has demonstrated the comparison operators in detail.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.