Windows PowerShell 1.0 Comparison and Containment Operators

From Techotopia
Revision as of 15:57, 26 November 2008 by Neil (Talk | contribs) (New page: == 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 wi...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents


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.

OperatorDescription
-eqEqual to (case insensitive)
-ieqEqual to (case insensitive)
-ceqEqual to (case sensitive)
-neNot equal to (case insensitive)
-ineNot equal to (case insensitive)
-cneNot equal to (case sensitive)
-gtGreater than (case insensitive)
-igtGreater than (case insensitive)
-cgtGreater than (case sensitive)
-geGreater than or equal to (case insensitive)
-igeGreater than or equal to (case insensitive)
-cgeGreater than or equal to (case sensitive)
-ltLess than (case insensitive)
-iltLess than (case insensitive)
-cltLess than (case sensitive)
-leLess than or equal to (case insensitive)
-ileLess than or equal to (case insensitive)
-cleLess than or equal to (case sensitive)
-containsGroup of values in left hand operand contains value specified in right hand operand (case insensitive)
-icontainsGroup of values in left hand operand contains value specified in right hand operand (case insensitive)
-ccontainsGroup of values in left hand operand contains value specified in right hand operand (case sensitive)
-notcontainsGroup of values in left hand operand does not contain value specified in right hand operand (case insensitive)
-inotcontainsGroup of values in left hand operand does not contain value specified in right hand operand (case insensitive)
-cnotcontainsGroup 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