Getting started with Office 365 PowerShell | Comparison and Logical operators | Part 3#4 5/5 (2) 8 min read

In the current article, we will review how to use PowerShell Comparison and Logical Operators that will help us to filter the PowerShell output result based on specific conditions that we define.

The Power of PowerShell | Article Series

The Power of PowerShell article series, including the following three articles:

In the following section, we review the how to use a combination of the PowerShell “Get” command + Comparison and Logical Operators for getting a “Sub Group” of objects from a particular object class array.

Before we begin, just a quick wrap up:

There are three “major “scenarios for using the “Get” command:

  1. Get information about a specific instance of an object. For example John Mailbox
  2. Get information about “Object class”. For example, the command Get-Mailbox, will display information about all the existing Mailboxes.
  3. Get information about “Sub Group” of an object from a specific object class.

For example, we want to display information about User Mailboxes their title is “Manager”.

In other words, we ask from PowerShell to perform for us the following “steps”: Get a list of all the existing users,
“Scan” or “check” each of the user properties named: “Title” and find a value that equal to “Manger” Display a list of Mailboxes that “answer” this condition(have the value “Manager” in the “Title” property).

Comparison and Logical operators

The option of “Filtering” or “Relating” to a “Sub Group” of objects implemented by using a “Logical condition”. The “condition” created by using: Comparison and Logical operator.
The following list displays the available PowerShell Comparison and Logical operators:

PowerShell Comparison operator

  • -lt – Less than
  • -le – Less than or equal to
  • -gt – Greater than
  • -ge – Greater than or equal to
  • -eq – Equal to
  • -ne – Not equal to
  • -like – Like; use wildcards for pattern matching

PowerShell Logical operator

  • -not
  • -and
  • -or

Where-Object

An additional component that we use with a comparison operator and the Logical operator is the: Where-Object.

The Where-Object cmdlet, combined with the Comparison and Logical operates, enable us to create a PowerShell sentence that describes the required “condition.”

General Note – in the next examples, we will use the shortened version of the Where-Object cmdlet, by using the cmdlet- Where.

The structure of PowerShell sentence with Comparison and Logical operators

An example for PowerShell sentence that contain a “condition” could be:

Get-Mailbox | Where {$_.Property -“Value”}
  • In the first part of the PowerShell sentence we use the “Get” command with the name of the object class that we are intersected in. In our example the object class is: Mailbox
  • In the second part of the PowerShell sentence, we use the Where cmdlet, together with a Logical operator for “fetching” (filtering) only objects that answers a specific condition.

Filtering objects by using a comparison and Logical operator-01

We can add additional “section” to the PowerShell sentence which enables us to define which properties will be displayed.

PowerShell will command syntax:

Get-Mailbox | Where {$_.Property -“Value”} | Select Property1,Property2
  • In the third part of the PowerShell sentence we specify the properties that we want to display

Filtering objects by using a comparison and Logical operator-02

Using PowerShell Logical and Comparison operators examples

The possibilities that we have, when using Comparison and Logical operators are unlimited. We can “write” a simple PowerShell sentence that includes one condition or write a “complicated” PowerShell sentence that is built from a “combination” of conditions.
In the following section, we will demonstrate different variations of the way that we use Comparison and Logical Operators.

Example 1 – Using the comparison operator “eq”

Description: Exchange online enable us to create different types of “Mailboxes” such as User Mailbox, Resource Mailbox, and Shared Mailbox.

The mission: we want to display a list of all the Users Mailbox’s (we want to exclude or “don’t display” the other types of Mailboxes).

Comparison and Logical Operators: we will use the equal (eq) operator because we want to find (filter), only Mailboxes that have a value that equals to “UserMailbox.”

PowerShell command syntax:

Get-Mailbox | Where {$_.RecipientTypeDetails -eq “UserMailbox”}

PowerShell sentence structure
First part: by using Get-Mailbox cmdlet, we are “asking from PowerShell to“: go and find all of the Existing Mailboxes Type. (The first part is “separated” from the second part of the pipe character).

Second part – In the second part we describe the “criteria” or the “condition”.

  • By using the pipe character, we are “telling” to PowerShell: “Take the input from the first part and “do something” with this input.
  • The “criteria” that we use will have to be enclosed in curly braces.
    • Property name: the property name that we use is: “RecipientTypeDetails”. The $_ notation is used to:
      represent the default object (that is, the object being transferred across the pipeline).
    • Value name – the value name is: “UserMailbox”
  • The comparison operator that we use is “eq”. This is a shortened version of the term: equal. When using the “eq” comparison operator, we are looking for “Exact match”.

Example 1 - Using the comparison operator “eq” for displaying Mailbox type

Note: In the next examples, we use the “Get” command for: filtering information about “user objects” that have a specific value. The examples are based on the Get-User cmdlet.

Example 2 – Using the comparison operator “EQ” for displaying users from a specific Department”

The mission: we want to display a list of all Office 365 users that work in the “Sales” department.

Comparison and Logical Operators: we use the comparison operator “eq” because we want to get “Exact Match”. The criteria\condition that we want to create is: display user name, only if this user has the particular value (string)”sales” in the department property.

PowerShell command syntax:

Get-User | Where {$_.Department -eq “Sales”}

Example 2 - Using the comparison operator “eq” for displaying users from a specific Department

Example 3 – Using a combination of: Logical operator “OR” + Comparison operator “EQ”

The mission: we want to display a list of all Office 365 users who work in the “Sales” department or, users who work in the “Marketing” department.
Comparison and Logical Operators: to implement this requirement, will we use the comparison operator “EQ” with a combination of the Logical operator “OR”.

For each of the criteria, we “dedicate” Brackets, and all the “criteria’s” are enclosed by curly braces. After the first “criteria” we use the comparison operator “OR”. The output of this command will be users who match the first criteria (users’ from the Sales Department) or users who match the first criteria (users from the Marketing Department).

PowerShell command syntax:

Get-User | Where {($_.Department -eq “Sales”) -or ($_.Department -eq “Marketing”)}

Example 3 - Using a combination of Logical operator OR Comparison operator EQ

Example 4 – Using a combination of: Logical operator “AND” + Comparison operator “EQ”

The mission: we want to display a list of all Office 365 users that work in the Sales department, and their Title is: Manger.

Comparison and Logical Operators: notice that now, the “condition” is different. The users that we want to display have to answer two “criteria’s” at the same time: the users should work at the “Sales” department, and, need to have the Title: “Manger.”

PowerShell command syntax:

Get-User | Where {($_.Department -eq “Sales”) -and ($_.Title -eq “Manger”)}

Example 4 - Using a combination of Logical operator “AND” Comparison operator “EQ”

Example 5 – Using a combination of: Logical operator “AND”, “OR” + Comparison operator “EQ”

The mission: we want to display a list of all Office 365 users that work in the “Sales” department or the “Marketing” department, and their title is: “Manger”.
Comparison and Logical Operators: the “condition” is more complicated because we need to find all the users that their Title is: “Manger” from two different departments: the Sales department or the Marketing department.

PowerShell command syntax:

Get-User | Where {($_.Department -eq “Sales”) -or ($_.Department -eq “Marketing”) -and ($_.Title -eq “Manger”)}

Example 5 - Using a combination of Logical operator “AND”, “OR” + Comparison operator “EQ”

Example 6 – Using the Comparison operator “LIKE”

The mission: we want to display a list of all Office 365 users that work in the Sales department. In our organization, we have 2 Sales departments:

Comparison and Logical operators: technically, we can create two PowerShell “sentences,” one that include the criteria: Department=Seals-USA and additional PowerShell sentence that include the criteria: Department=Seals-UK.
Luckily, we can use a more efficient way, for “writing” these criteria. The “solution” could implement by using the comparison operator: “LIKE”. The “LIKE” comparison operator enables us to locate values that include or have a particular string.

In our example, we look for values that start with the “sales” string, but we “don’t care” about the rest of the string, or in other words: we look for a Partial Match.

When using the “LIKE” comparison operator, we need to add the “Asterisk” (*) character. The Asterisks character serves as a “wild card”. When using the Asterisks character at the end of the string, PowerShell will find all of the values that start with the string “Sales.”

PowerShell command syntax:

Get-User | Where {$_.Department -like “Sales*”}

Example 6 - Using the Comparison operator “LIKE”

Example 7 – Using the Comparison operator “NE”

Description: the corporate headquarters located in the USA, and most of the organization users, located in the USA. Additionally, the company has six branches located in Europe (Spain, England, etc.)

The mission: we want to get a list of all the organization users, that don’t work in the corporate headquarters.

Comparison and Logical Operators: technically, we can write a PowerShell sentence that includes six comparison operators: “OR” to get the required information.

More efficient option for writing this condition is: by using the comparison operator: “NE” (not equal). Using the comparison operator: “NE” is like using: “invert selection”. In other words, we are “asking” from PowerShell to find all the objects that don’t have the value: USA.

PowerShell command syntax:

Get-User | Where {$_.StateOrProvince -ne “USA”}

Example 7 - Using the Comparison operator “NE”

The Power of PowerShell – Office 365 | Article series index

Now it’s Your Turn!
It is important for us to know your opinion on this article


Print Friendly, PDF & Email

Related Post

Please rate this

Eyal Doron on EmailEyal Doron on FacebookEyal Doron on GoogleEyal Doron on LinkedinEyal Doron on PinterestEyal Doron on RssEyal Doron on TwitterEyal Doron on WordpressEyal Doron on Youtube
Eyal Doron
Share your knowledge.
It’s a way to achieve immortality.
Dalai Lama

One Response to “Getting started with Office 365 PowerShell | Comparison and Logical operators | Part 3#4”

  1. Read all three posts, it’s great..need more like how to export to excel and then import using csv file.

Leave a Reply

Your email address will not be published. Required fields are marked *