Updated March 23, 2023
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.
Comparison Operators
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.
-eq | Equal |
-ne | Not Equal |
-gt | Greater than |
-ge | Greater than or equal to |
-lt | Less than |
-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) |
-match | RegEx comparison |
-notmatch | RegEx comparison |
-contains | Containment Operator |
-notcontains | Non-Containment Operator |
-In | In Operator |
-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.
7. –Like
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.
8. –NotLike
It is contrary to Like operator but definition remains the same for a wildcard, and only output is reversed.
9. –Match
It matches the string using a regular expression. When the input is scalar, it populates $matches variable automatically.
$matches
Name Value
—- —–
0 coming
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.
10. –NotMatch
Contrary to Match operator. If match found then returns false or vice versa.
Name Value
—- —–
0 coming
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.
Syntax:
<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.
13. -in
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.
Syntax:
<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
Syntax:
<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
-bAnd | Bitwise AND |
-bOr | Bitwise OR (Inclusive) |
-bXor | Bitwise OR (Exclusive) |
-bNot | Bitwise NOT |
-shl | Shift-Left |
-shr | Shift-Right |
2. Logical Operators
-and | Logical AND |
-or | Logical OR |
-xor | Logical Exclusive OR |
-not | Logical NOT |
! | Logical NOT |
Recommended Articles
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-