Using Windows PowerShell as an IT Pro – Part 16
In my last post I looked at Assignment Operators. Now I will examine Comparison Operators.Comparison operators let you specify conditions for comparing values and finding values that match specified patterns. To use a comparison operator, specify the values that you want to compare together with an operator that separates these values.
By default, all comparison operators are case-insensitive. To make a comparison operator case-sensitive, precede the operator name with a "c". For example, the case-sensitive version of "-eq" is "-ceq". To make the case-insensitivity explicit, precede the operator with an "i". For example, the explicitly case-insensitive version of "-eq" is "ieq".
All comparison operators except the containment operators (-contains, -notcontains) and type operators (-is, -isnot) return a Boolean value when the input to the operator (the value on the left side of the operator) is a single value (a scalar). When the input is a collection of values, the containment operators and the type operators return any matching values. If there are no matches in a collection, these operators do not return anything. The containment operators and type operators always return a Boolean value.
Windows PowerShell supports the following comparison operators.
Operator | Description |
-eq | Equal to. Includes an identical value. |
-ne | Not equal to. Includes a different value. |
-gt | Greater-than. |
-ge | Greater-than or equal to. |
-lt | Less-than. |
-le | Less-than or equal to. |
-like | Match using the wildcard character (*). |
-notlike | Does not match using the wildcard character (*). |
-match | Matches a string using regular expressions. When the input is scalar, it populates the $Matches automatic variable. |
-notmatch | Does not match a string. Uses regular expressions. When the input is scalar, it populates the $Matches automatic variable. |
-contains | Containment operator. Includes an identical value that is not part of a value. Always returns a Boolean value. |
-notcontains | Containment operator. Does not include an identical value. Always returns Boolean. |
-replace | Replace operator. Changes the specified elements of a value. |
The equality operators (-eq, -ne) return a value of TRUE or matches when one or more of the input values is identical to the specified pattern. The entire pattern must match an entire value.
The following examples show the effect of the equal to operator:
"a" -eq "A"
"a" -ceq "A"
1, 2, 3 -eq 2
"Power" -ne "Shell"
In my next post we will examine more Comparison Operators.
No comments:
Post a Comment