You are on page 1of 7

TOPIC

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.
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.
-eq
Description: Equal to. Includes an identical value.
Example:
C:\PS> "abc", "def" -eq "abc"
abc
-ne
Description: Not equal to. Includes a different value.
Example:
C:\PS> "abc", "def" -ne "abc"
def
-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:
C:\PS> "Windows PowerShell" -like "*shell"
True
-notlike
Description: Does not match using the wildcard character (*).
Example:
C:\PS> "Windows PowerShell" -notlike "*shell"
False
-match
Description: Matches a string using regular expressions.
When the input is scalar, it populates the
$Matches automatic variable.
Example:
C:\PS> "Sunday" -match "sun"
True
C:\PS> $matches
Name Value
---- ----0
sun

-notmatch
Description: Does not match a string. Uses regular expressions.
When the input is scalar, it populates the $Matches
automatic variable.
Example:
C:\PS> "Sunday" -notmatch "sun"
False
C:\PS> $matches
Name Value
---- ----0
sun
-contains
Description: Containment operator. Includes an identical value that is
not part of a value. Always returns a Boolean value.
Example:
C:PS> "abc", "def" -contains "def"
True
-notcontains
Description: Containment operator. Does not include an identical value.
Always returns Boolean.
Example:
C:PS> "Windows", "PowerShell" -notcontains "Shell"
True
-replace
Description: Replace operator. Changes the specified elements of a value.
Example:
C:\PS> "Get-Process" -replace "Get", "Stop"
Stop-Process
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.
The following examples show the effect of the equal to operator:
C:PS> 1,2,3 -eq 2
2
C:PS> "PowerShell" -eq "Shell"

False
C:PS> "Windows", "PowerShell" -eq "Shell"
C:PS>
C:\PS> "abc", "def", "123" -eq "def"
def
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.
Also, unlike the equality operators, the containment operators return a
value as soon as they detect the first match. The equality operators
evaluate all input and then return all the matches in the collection.
The following examples show the effect of the -contains operator:
C:PS> 1,2,3 -contains 2
True
C:PS> "PowerShell" -contains "Shell"
False
C:PS> "Windows", "PowerShell" -contains "Shell"
False
C:\PS> "abc", "def", "123" -contains "def"
True
C:\PS> "true", "blue", "six" -contains "true"
True
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.
C:\PS> 1,2,3,4,5,4,3,2,1 -eq 2
2
2
C:\PS> 1,2,3,4,5,4,3,2,1 -contains 2
True
In a very large collection, the -contains operator returns results
quicker than the equal to operator.
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 following examples show some uses of the -match operator:
C:\PS> "Windows", "PowerShell" -match ".shell"
PowerShell
C:\PS> (get-command get-member -syntax) -match "-view"
True
C:\PS> (get-command get-member -syntax) -notmatch "-path"
True
C:\PS> (get-content servers.txt) -match "^Server\d\d"
Server01
Server02
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>
In contrast, the following command submits a single string to the
-match operator. The -match operator returns a Boolean value and
populates the $Matches automatic variable.
C:\PS> "Sunday" -match "sun"
True
C:\PS> $matches
Name
---0

Value
----Sun

The -notmatch operator populates the $Matches automatic variable when


the input is scalar and the result is False, that it, when it detects
a match.
C:\PS> "Sunday" -notmatch "rain"
True
C:\PS> $matches
C:\PS>
C:\PS> "Sunday" -notmatch "day"
False
C:\PS> $matches
C:\PS>
Name
---0

Value
----day

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:
Get-ChildItem | Rename-Item -NewName { $_ -replace '.gif$','.jpg$' }
The syntax of the -replace operator is as follows, where the <original>
placeholder represents the characters to be replaced, and the
<substitute> placeholder represents the characters that will replace
them:
<input> <operator> <original>, <substitute>
By default, the -replace operator is case-insensitive. To make it case
sensitive, use -creplace. To make it explicitly case-insensitive, use
-ireplace. Consider the following examples:
C:\PS> "book" -replace "B", "C"
Cook
C:\PS> "book" -ireplace "B", "C"
Cook
C:\PS> "book" -creplace "B", "C"
book
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.


Windows PowerShell supports the following bitwise operators.
Operator Description
-------- ----------------------band
Bitwise AND

Example
------------------C:\PS> 10 -band 3
2

-bor

Bitwise OR (inclusive)

C:\PS> 10 -bor 3
11

-bxor

Bitwise OR (exclusive)

C:\PS> 10 -bxor 3
9

Bitwise operators act on the


bit structure for the number
bit structure for the number
operator to compare 10 to 3,
compared.

binary format of a value. For example, the


10 is 00001010 (based on 1 byte), and the
3 is 00000011. When you use a bitwise
the individual bits in each byte are

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)
In a bitwise OR (inclusive) operation, the resulting bit is set to 1
when either or both input bits are 1. The resulting bit is set to 0 only
when both input bits are set to 0.
00001010
(10)
00000011
( 3)
------------------ bOR (inclusive)
00001011
(11)
In a bitwise OR (exclusive) operation, the resulting bit is set to 1 only
when one input bit is 1.
00001010
(10)
00000011
( 3)
------------------ bXOR (exclusive)
00001001
( 9)
SEE ALSO
about_Operators
about_Regular_Expressions
about_Wildcards
Compare-Object

You might also like