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.
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.
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
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
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
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
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
© m365corner.com. All Rights Reserved. Design by HTML Codex