Make Decisions with Comparison and Logical Operators

1. Problem

You want to compare some data with other data and make a decision based on that comparison.

2. Solution

Use PowerShell’s logical operators to compare pieces of data and make decisions based on them.

Comparison operators:

-eq, -ne, -ge, -gt, -lt, -le, -like, -notlike, -match, -notmatch, -contains, -notcontains, -is, -isnot

Logical operators:

-and, -or, -xor, -not

For a detailed description (and examples) of these operators, see “Comparison Operators” in Appendix A.

3. Discussion

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

For a detailed description of how the individual comparison operators adapt to their input, see “Comparison Operators” in Appendix A.

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.

Tags: , , , , , , , , ,