fbpx Make Decisions with Comparison and Logical Operators in Windows PowerShell | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Make Decisions with Comparison and Logical Operators in Windows PowerShell

Problem

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

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

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

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.

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.

For more information about PowerShell’s operators, type GetHelp About_Operator.

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say