Introduction to Comparison Operators in PowerShell
In this article, we will see in detail different Comparison Operators in PowerShell. Comparison operators are used to compare, search and alter two or more values. You can also use comparison operators in conditions to match and compare values.
Windows PowerShell uses below comparison operators and by default they are Case-Insensitive. To perform a case-sensitive operation, just need to type ‘c’ ahead of the below operators. For example, -clike, -cne, -ceq etc.
|-ge||Greater than or equal to|
|-le||Less than or equal to|
|-like||Checks if part of string matches (Wildcard comparison)|
|-notlike||Checks if part of a string doesn’t matches (Wildcard comparison)|
|-notIn||Non In Operator|
|-Replace||Replaces a string pattern|
Examples to implement comparison operators
Here are some of the examples of comparison operator given below with examples:
1. –eq: Equal to
This operator is used to check equality between values. They should match exactly and this is case-insensitive. The output will be True or False. For example,
You can also compare two different data types.
You can compare multiple values with a single value as well.
If you use –ceq operator then the comparison will be case-sensitive.
2. –ne: Not Equal to
This is contrary to equal to Operation. If value matches then it will return FALSE, otherwise it will return TRUE.
For multiple values,
3. –gt: Greater than
It compares the value of its left side with the right side and output will be TRUE or FALSE based on values. In the example below, it compares 5 with 6 and checks if it’s greater than 6 and provides answer FALSE.
When you compare two characters, it checks its ASCII value and provides results based on it. ASCII value of ‘a’ is 97 and ‘c’ is 99, so ‘c’ is greater than ‘a’. As a result, the second sentence is TRUE.
If you compare multiple characters together then it will check the ASCII value of first characters on both sides and provide results accordingly. For example,
In the above first sentence, ASCII value of ‘d’ is greater than ‘a’ and in the second sentence, the ASCII value of ‘a is less than ‘b’. For multiple value comparison.
4. –ge: Greater than equal to
Checks if the value of the left side is greater than or equal to the value of the right side.
5. –lt: Less than
Check if the value of the left side is less than the value of the right side.
6. –le: Less than or equal to
Checks if the value of the left side is less than equal to the value of the right-side value.
It matches the left side of the value with the right side value with Wildcard character (*).
Wildcard character (*), when applied it checks if part contains in a string or not. If it applied after particular value (i.e. This*) then it checks if that word + successor string exist or not. In the first example.
- Word: This
- Successor String: is Powershell
If applied before the particular word then it checks if a particular word + precedence string exists or not. In the second example.
- Word: Powershell
- Precedent string: This is
In the third example, wildcard (*) is applied afterword “Powershell” and word is matching but there is no successor string so the output is False. In the fourth example, wildcard (*) is applied both sides so it checks if a string exists before or after it in other words mentioned word is part of that string. So here output is TRUE.
It is contrary to Like operator but definition remains the same for a wildcard, and only output is reversed.
It matches the string using a regular expression. When the input is scalar, it populates $matches variable automatically.
To match at least any one of the characters, put them into [ ].
In the above example, the character ‘c’ is matching. To match at least one of the characters in the contiguous range [range].
Here, from characters n to r [case-insensitive] will be checked against a string and ‘o’ and ’p’ are matching. Hence, the output is TRUE.
Contrary to Match operator. If match found then returns false or vice versa.
11. -Contains: Containment Operator
Tells whether a collection of reference values includes the exact (case-insensitive) single value. If yes then returns TRUE, otherwise FALSE.
<Reference-values> -Contains <Test-value>
The above output is FALSE because the left side contains two values. But if you store them into a variable and match them, the output will be TRUE.
For case-sensitive operations, use –ccontains operator.
12. –notContains: Containment Operator (Contrary to Contains)
If the test value exactly matches the set of reference values then output is FALSE. Otherwise, the output is TRUE.
Similar to Contain Operator, only the syntax is reversed. -in the operator was introduced in Powershell 3.0. If the test value matches the reference values then returns TRUE otherwise FALSE.
<Test-value> -in <Reference-values>
In the above example, “Shell” doesn’t exactly match with Powershell.
14. –notin: Not in Operator (Contrary to -in operator)
If test value matches exactly to Reference values then it returns FALSE, otherwise TRUE.
15. –Replace: Changes the specified value
<String> -replace <Old value>,<New Value>
To replace the value in a variable
16. Other Similar Operators
There are other similar operators as mentioned below.
a. Bitwise Operators
|-bOr||Bitwise OR (Inclusive)|
|-bXor||Bitwise OR (Exclusive)|
2. Logical Operators
|-xor||Logical Exclusive OR|
This is a guide to Comparison Operators in PowerShell. Here we discuss the different types of comparison operators in Powershell along with examples. You may also look at the following articles to learn more-