Changes

Basic Windows PowerShell 1.0 Operators

4,866 bytes removed, 15:55, 26 November 2008
no edit summary
d-r-- 11/7/2008 4:26 PM Documents
d-r-- 11/7/2008 4:26 PM Downloads
</pre>
 
== 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.
 
<table border="1" cellspacing="0">
<tr bgcolor="#cccccc" style="color:black" valign="top">
<th>Operator</th><th>Description</th>
<tr>
<td>-eq<td>Equal to (case insensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-ieq<td>Equal to (case insensitive)</td>
<tr>
<td>-ceq<td>Equal to (case sensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-ne<td>Not equal to (case insensitive)</td>
<tr>
<td>-ine<td>Not equal to (case insensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-cne<td>Not equal to (case sensitive)</td>
<tr>
<td>-gt<td>Greater than (case insensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-igt<td>Greater than (case insensitive)</td>
<tr>
<td>-cgt<td>Greater than (case sensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-ge<td>Greater than or equal to (case insensitive)</td>
<tr>
<td>-ige<td>Greater than or equal to (case insensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-cge<td>Greater than or equal to (case sensitive)</td>
<tr>
<td>-lt<td>Less than (case insensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-ilt<td>Less than (case insensitive)</td>
<tr>
<td>-clt<td>Less than (case sensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-le<td>Less than or equal to (case insensitive)</td>
<tr>
<td>-ile<td>Less than or equal to (case insensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-cle<td>Less than or equal to (case sensitive)</td>
<tr>
<td>-contains<td>Group of values in left hand operand contains value specified in right hand operand (case insensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-icontains<td>Group of values in left hand operand contains value specified in right hand operand (case insensitive)</td>
<tr>
<td>-ccontains<td>Group of values in left hand operand contains value specified in right hand operand (case sensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-notcontains<td>Group of values in left hand operand does not contain value specified in right hand operand (case insensitive)</td>
<tr>
<td>-inotcontains<td>Group of values in left hand operand does not contain value specified in right hand operand (case insensitive)</td>
<tr bgcolor="#e9e9e6">
<td>-cnotcontains<td>Group of values in left hand operand does not contain value specified in right hand operand (case sensitive)</td>
<tr>
</table>
 
== 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:
 
<pre>
PS C:\Users\Administrator> 10 -eq 20
False
</pre>
 
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:
 
<pre>
PS C:\Users\Administrator> 10 -eq "10"
True
</pre>
 
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:
 
<pre>
PS C:\Users\Administrator> "hello" -eq "HELLO"
True
PS C:\Users\Administrator> "hello" -ieq "HELLO"
True
</pre>
 
Conversely, the folloing command performs a case sensitive comparison of the same two string:
 
<pre>
PS C:\Users\Administrator> "hello" -ceq "HELLO"
False
</pre>
 
== 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:
 
<pre>
PS C:\Users\Administrator> $myarray = "red, "yellow", "blue", "yellow"
PS C:\Users\Administrator> $myarray -eq "yellow"
yellow
yellow
</pre>
 
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 comamnd returns a value of ''True'' because an array element does, indeed, contain the value "red":
 
<pre>
PS C:\Users\Administrator> $myarray = "red, "yellow", "blue", "yellow"
PS C:\Users\Administrator> $myarray -contains "red"
True
</pre>