Using -Like Operator in Graph PowerShell

The -like operator in Graph PowerShell performs pattern matching when querying Microsoft 365 resources. This operator allows M365 administrators to filter results based on specific string patterns. Whether you're retrieving M365 user information, filtering group names, or managing device details, the -like operator in Graph PowerShell becomes an essential tool for efficient and targeted M365 administration.


Basic Syntax

The basic syntax for the -like operator is: <string> -like <pattern>. Example Usage: 'Hello World' -like 'Hello*' returns True because the string "Hello World" starts with "Hello". 'Hello World' -like '*World' also returns True because the string "Hello World" ends with "World".

Note: The -like operator is case-insensitive by default, making it very convenient for searches where case sensitivity isn't a concern. If you need case-sensitive matching, you would use the -clike operator.


Search Users by Mail

Suppose you want to find all users in your organization who belong to a specific domain (e.g., "7xh7fj.onmicrosoft.com"). You can use the Get-MgUser cmdlet in combination with Where-Object and -like operator to fetch the required domain users.

You can download the script here: search-users-by-mail.ps1


Search Users by Job Title

Suppose you want to fetch all the users with job title 'engineer', you can do so by using Get-MgUser cmdlet in combination with Where-Object and -like operator as shown in the script.

You can download the script here: search-users-by-job-title.ps1


Using -like in If-Else Condition

Suppose you want differentiate between the Microsoft 365 groups in your tenant (based on their DisplayName) and display the results, you can do so by using Get-MgGroup cmdlet in combination with if-else statement and -like operator.

You can download the script here: get-groups.ps1


Using -like with AND,OR operators

You can also use -like operator in combination with AND/OR logical operators to make your Graph PowerShell scripts that query Microsoft 365 tenant more effective. The below script loops through all available teams within the M365 tenant and displays a result depending upon the group DisplayName.

Sample Script Output for your Reference

You can download the script here: get-teams.ps1


Using -like with Switch Statement

You can also use -like operator within Switch statements. The below script uses -like operator within Switch statement to look for users of specific domain and displays them on the console.

You can download the script here: get-domain-users.ps1


Usage Considerations

  • Performance: Using these operators in scripts requires pulling data into memory and processing it locally. This might be inefficient for large datasets, so use server-side filtering (if available) to reduce the dataset before applying these conditions.
  • API Limitations: Always be aware of what filtering capabilities the Microsoft Graph API itself supports. Using local processing (like with if conditions) should be a backup or supplementary method when API-level filtering is not sufficient.

Related Articles:

Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex