Windows PowerShell 1.0 Comparison and Containment Operators
Previous | Table of Contents | Next |
Basic Windows PowerShell 1.0 Operators | Windows PowerShell 1.0 Pipes and Redirection |
<google>BUY_WPS</google>
The previous chapter (entitled Basic Windows PowerShell 1.0 Operators) looked at the basic arithmetic operators provided in Windows PowerShell. In this chapter we will begin to look at some slightly more advanced operators in the form of comparison and containment operators.
Windows PowerShell Comparison Operators
Windows PowerShell provides a range of comparison operators which can be used to compare one value with another.
These operators work both with string and number based values assuming that you are comparing like with like (it is not meaningful, for example, to compare a string with numerical value). In terms of working with strings and characters, both case-sensitive and insensitive operators are supported. <google>ADSDAQBOX_FLOW</google>
Operator | Description |
---|---|
-eq | Equal to (case insensitive) |
-ieq | Equal to (case insensitive) |
-ceq | Equal to (case sensitive) |
-ne | Not equal to (case insensitive) |
-ine | Not equal to (case insensitive) |
-cne | Not equal to (case sensitive) |
-gt | Greater than (case insensitive) |
-igt | Greater than (case insensitive) |
-cgt | Greater than (case sensitive) |
-ge | Greater than or equal to (case insensitive) |
-ige | Greater than or equal to (case insensitive) |
-cge | Greater than or equal to (case sensitive) |
-lt | Less than (case insensitive) |
-ilt | Less than (case insensitive) |
-clt | Less than (case sensitive) |
-le | Less than or equal to (case insensitive) |
-ile | Less than or equal to (case insensitive) |
-cle | Less than or equal to (case sensitive) |
Windows PowerShell Containment Operators
In addition, to the comparison operators, a set of operators referred to as containment operators are provided for the purpose of identifying whether an array or collection contains a specific value. The containment operators provided by PowerShell are listed in the following table. As with the comparison operators, both case sensitive and insensitive options are provided.
Operator | Description |
---|---|
-contains | Group of values in left hand operand contains value specified in right hand operand (case insensitive) |
-icontains | Group of values in left hand operand contains value specified in right hand operand (case insensitive) |
-ccontains | Group of values in left hand operand contains value specified in right hand operand (case sensitive) |
-notcontains | Group of values in left hand operand does not contain value specified in right hand operand (case insensitive) |
-inotcontains | Group of values in left hand operand does not contain value specified in right hand operand (case insensitive) |
-cnotcontains | Group of values in left hand operand does not contain value specified in right hand operand (case sensitive) |
Performing Windows PowerShell Comparisons
When using the Windows PowerShell comparison operators, it is important to keep in mind that PowerShell will look at the left hand operand of the expression to decide the value type to be used as the basis of the comparison. Having made this decision, if the right hand operand is of a different type PowerShell will attempt to convert it. This concept is probably best described using some examples.
In the following example, both operands are of the same type, so no conversion is performed:
PS C:\Users\Administrator> 10 -eq 20 False
If, however, the operands are of different types a conversion of the right hand operand will be necessary. In the following example, the right hand operand will be converted to a number before the comparison is performed:
PS C:\Users\Administrator> 10 -eq "10" True
Both case sensitive and case insensitive comparison operators are available when comparing strings and characters. For example, the following commands both perform a case insensitive comparison:
PS C:\Users\Administrator> "hello" -eq "HELLO" True PS C:\Users\Administrator> "hello" -ieq "HELLO" True
Conversely, the folloing command performs a case sensitive comparison of the same two string:
PS C:\Users\Administrator> "hello" -ceq "HELLO" False
Using Comparison Operators with Arrays and Collections
The Windows PowerShell comparison operators can be used with both arrays and collections. In the case of the basic comparison operators, PowerShell will return all elements which contain the value specified in the right hand operand. For example:
PS C:\Users\Administrator> $myarray = "red, "yellow", "blue", "yellow" PS C:\Users\Administrator> $myarray -eq "yellow" yellow yellow
Using PowerShell Containment Operators
Alternatively, the containment operators may be used to obtain a true or false result depending on whether a collection or array contains a specified value. The following command returns a value of True because an array element does, indeed, contain the value "red":
PS C:\Users\Administrator> $myarray = "red, "yellow", "blue", "yellow" PS C:\Users\Administrator> $myarray -contains "red" True