Professional Documents
Culture Documents
About Comparison Operators - Help
About Comparison Operators - Help
about_Comparison_Operators
SHORT DESCRIPTION
Describes the operators that compare values in Windows PowerShell.
LONG DESCRIPTION
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.
-eq
Description: Equal to. Includes an identical value.
Example:
-ne
Description: Not equal to. Includes a different value.
Example:
-gt
Description: Greater-than.
Example:
C:\PS> 8 -gt 6
True
-ge
Description: Greater-than or equal to.
Example:
C:\PS> 8 -ge 8
True
-lt
Description: Less-than.
Example:
C:\PS> 8 -lt 6
False
-le
Description: Less-than or equal to.
Example:
C:\PS> 6 -le 8
True
-like
Description: Match using the wildcard character (*).
Example:
-notlike
Description: Does not match using the wildcard character (*).
Example:
-match
Description: Matches a string using regular expressions.
When the input is scalar, it populates the
$Matches automatic variable.
Example:
-contains
Description: Containment operator. Includes an identical value that is
not part of a value. Always returns a Boolean value.
Example:
-notcontains
Description: Containment operator. Does not include an identical value.
Always returns Boolean.
Example:
-replace
Description: Replace operator. Changes the specified elements of a value.
Example:
Equality Operators
The equality operators (-eq, -ne) return a value of TRUE or the matches
when one or more of the input values is identical to the specified
pattern. The entire pattern must match an entire value.
Containment Operators
The containment operators (-contains and -notcontains) are similar to the
equality operators. However, the containment operators always return a
Boolean value, even when the input is a collection.
The following example shows how the containment operators differ from the
equal to operator. The containment operators return a value of TRUE on the
first match.
Match Operators
The match operators (-match and -notmatch) find elements that match or
do not match a specified pattern using regular expressions.
The syntax is:
<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>
The match operators search only in strings. They cannot search in arrays
of integers or other objects.
The -match and -notmatch operators populate the $Matches automatic
variable when the input (the left-side argument) to the operator
is a single scalar object. When the input is scalar, the -match and
-notmatch operators return a Boolean value and set the value of the
$Matches automatic variable to the matched components of the argument.
If the input is a collection, the -match and -notmatch operators return
the matching members of that collection, but the operator does not
populate the $Matches variable.
For example, the following command submits a collection of strings to
the -match operator. The -match operator returns the items in the collecti
on
that match. It does not populate the $Matches automatic variable.
C:\PS> "Sunday", "Monday", "Tuesday" -match "sun"
Sunday
C:\PS> $matches
C:\PS>
Replace Operator
The -replace operator replaces all or part of a value with the specified
value using regular expressions. You can use the -replace operator for
many administrative tasks, such as renaming files. For example, the
following command changes the file name extensions of all .gif files
to .jpg:
Bitwise Operators
Windows PowerShell supports the standard bitwise operators, including
bitwise-AND (-band), and inclusive and exclusive bitwise-OR operators
(-bor and -bxor). Beginning in Windows PowerShell 2.0, all bitwise
operators work with 64-bit integers.
In a bitwise AND operation, the resulting bit is set to 1 only when both
input bits are 1.
00001010 (10)
00000011 ( 3)
------------------ bAND
00000010 ( 2)
00001010 (10)
00000011 ( 3)
------------------ bOR (inclusive)
00001011 (11)
00001010 (10)
00000011 ( 3)
------------------ bXOR (exclusive)
00001001 ( 9)
SEE ALSO
about_Operators
about_Regular_Expressions
about_Wildcards
Compare-Object