Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
You want to compare some data with other data and make a decision based on that comparison.
Use PowerShell’s logical operators to compare pieces of data and make decisions based on them:
-eq
, -ne
, -ge
, -gt
, -in
, -notin
, -lt
, -le
, -like
, -notlike
, -match
, -notmatch
,
-contains
, -notcontains
, -is
, -isnot
-and
, -or
, -xor
, -not
For a detailed description (and examples) of these operators, see “Comparison Operators”.
PowerShell’s logical and comparison operators let you compare pieces of data or test data for some condition. An operator either compares two pieces of data (a binary operator) or tests one piece of data (a unary operator). All comparison operators are binary operators (they compare two pieces of data), as are most of the logical operators. The only unary logical operator is the -not
operator, which returns the true/false
opposite of the data that it tests.
Comparison operators compare two pieces of data and return a result that depends on the specific comparison operator. For example, you might want to check whether a collection has at least a certain number of elements:
PS > (dir).Count -ge 4 True
or check whether a string matches a given regular expression:
PS > "Hello World" -match "H.*World" True
Most comparison operators also adapt to the type of their input. For example, when you apply them to simple data such as a string, the -like
and -match
comparison operators determine whether the string matches the specified pattern. When you apply them to a collection of simple data, those same comparison operators return all elements in that collection that match the pattern you provide.
The -match
operator takes a regular expression as its argument. One of the more common regular expression symbols is the $
character, which represents the end of line. The $
character also represents the start of a PowerShell variable, though! To prevent PowerShell from interpreting characters as language terms or escape sequences, place the string in single quotes rather than double quotes:
PS > "Hello World" -match "Hello" True PS > "Hello World" -match 'Hello$' False
By default, PowerShell’s comparison operators are case-insensitive. To use the case-sensitive versions, prefix them with the character c
:
-ceq
,-cne
,-cge
,-cgt
,-cin
,-clt
,-cle
,-clike
,-cnotlike
,-cmatch
,-cnotmatch
,-ccontains
,-cnotcontains
For a detailed description of the comparison operators, their case-sensitive counterparts, and how they adapt to their input, see “Comparison Operators”.
Logical operators combine true
or false
statements and return a result that depends on the specific logical operator. For example, you might want to check whether a string matches the wildcard pattern you supply and that it is longer than a certain number of characters:
PS > $data = "Hello World" PS > ($data -like "*llo W*") -and ($data.Length -gt 10) True PS > ($data -like "*llo W*") -and ($data.Length -gt 20) False
Some of the comparison operators actually incorporate aspects of the logical operators. Since using the opposite of a comparison (such as -like
) is so common, PowerShell provides comparison operators (such as -notlike
) that save you from having to use the -not
operator explicitly.
For a detailed description of the individual logical operators, see “Comparison Operators”.
Comparison operators and logical operators (when combined with flow control statements) form the core of how we write a script or command that adapts to its data and input.
See also “Conditional Statements” for detailed information about these statements.
For more information about PowerShell’s operators, type Get-Help
about_Operators
.
“Comparison Operators”
“Conditional Statements”