Understanding PowerShell Operators

Now that we are starting to look at more complex logic statements, our variables need to be evaluated somehow. In this tutorial, you will learn of the various operators in PowerShell.

Setup

If you have not already done so, click open Windows PowerShell ISE.

The Concept…

There are different forms of operator: Comparison and Logical. Comparison operators take two variables or objects and compare them. The comparison operators are listed in the chart below:

Operator

Description

-eq

Equal to

-lt

Less than

-gt

Greater than

-ge

Greater than or Equal to

-le

Less than or equal to

-ne

Not Equal to

-is

Returns TRUE if value is a certain data type

-isnot

Returns TRUE if the value is not a certain data type

-like

Like (uses Wildcard for pattern matching)

-notlike

Not like (uses wildcard for pattern searching)

-match

A match using regular expressions

-contains

Used to see whether a collection or group of items contains a given term.

-notcontains

Used to see whether a collection or group of items does not contain a given item.

Logical operators say what variables are or are not equal to. Or if they can be either or. We may want variable $i to equal or not to equal something in a conditional loop. For example, while variable $i = 3 –or 9 –or 1, print out to the system, "You have a factor of nine!". The example in reality could use quite a bit of logic before it actually works accurately, but that is an example of how the logical operators would work. The logical operators are listed in the chart below.

Operator

Description

-not

Not

!

Not

-and

And

-or

Or

Remarks last but not least…

This was merely a small glimpse on what operators are available in PowerShell. These operators will be used quite frequently throughout the tutorials on the site. Join us next time for additional Windows PowerShell tutorials! Till then…