Using -Filter in Graph PowerShell for Microsoft 365

Mastering the -Filter parameter in Graph PowerShell is essential for efficient Microsoft 365 management. In this guide, we'll explore powerful filter techniques to query your M365 tenant with precision. Boost your productivity and streamline your M365 tasks with these expert Graph PowerShell tips.


Basic Syntax

Here’s the basic syntax for -Filter usage: Get-MgUser -Filter "property operator 'value'"

  1. Get-MgUser: This cmdlet is used to retrieve user objects from Microsoft 365. You can replace this with other cmdlets depending on the type of object you are querying (e.g., Get-MgGroup for groups).
  2. -Filter: This parameter specifies the filtering criteria to narrow down the results based on specific properties.
  3. "property operator 'value'": This is the actual filter expression:
    • property: The attribute you want to filter on (e.g., displayName, jobTitle, department).
    • operator: The comparison operator (e.g., eq for equal, ne for not equal, gt for greater than, lt for less than).
    • 'value': The value you are comparing the property against. Always enclose the value in single quotes.


Find Users by Department

To get all users who belong to a specific department, use the -Filter parameter to filter by the department attribute.

Graph PowerShell command filtering Microsoft 365 users by department

Find Users by Job Title

You can filter users based on their job title to find all users who hold a particular position.

Graph PowerShell command filtering Microsoft 365 users by job title

Find Users Based On Country

To find all users located in a specific country or region, you can filter by the country attribute.

Graph PowerShell command filtering Microsoft 365 users by country

Find Disabled User Accounts

To find all user accounts that are currently disabled, filter by the accountEnabled attribute.

Graph PowerShell command retrieving disabled Microsoft 365 user accounts

Find Users Created After a Specific Date

To get users who were created after a certain date, use the createdDateTime attribute with a date filter.

Graph PowerShell command retrieving Microsoft 365 users created after a specific date

Find Users Based on Sign-in Activity

To find users who haven't signed in for a specific period, use the signInActivity attribute (requires Azure AD Premium P1 or P2 license).

Graph PowerShell command retrieving Microsoft 365 users based on sign-in activity

Find Groups by Display Name

To find groups with a specific display name, use the displayName attribute.

Graph PowerShell command filtering Microsoft 365 groups by display name

Find Users by Mail

To find users by mail, use the mail attribute in your filter.

Graph PowerShell command filtering Microsoft 365 users by mail attribute

Frequently Asked Questions

  • Can I use any property with the -Filter parameter?
  • No, only a subset of Microsoft Graph properties support filtering. If you attempt to filter on unsupported properties, you’ll receive a Request_UnsupportedQuery error. Always check the Microsoft Graph documentation for filterable fields.

  • How is -Filter different from Where-Object in PowerShell?
  • -Filter executes the query server-side in Microsoft Graph, returning only the matching objects. In contrast, Where-Object applies filtering locally after all results are retrieved, which can be slower and more resource-intensive.

  • Do I need to use quotes in my filter string?
  • Yes, string values must be enclosed in single quotes (e.g., "department eq 'Finance'"). Missing quotes or using double quotes inside the filter often leads to syntax errors.

  • Can I combine multiple conditions in one filter?
  • Yes, you can chain conditions with and or or. For example:

    Get-MgUser -Filter "accountEnabled eq true and startswith(displayName,'A')"

    This returns only enabled users whose display name begins with “A”.


Tips for Using Filter with Microsoft Graph PowerShell

  • Enclose Values in Single Quotes: Always enclose string values in single quotes within the filter expression to avoid syntax errors.
  • Combine Multiple Filters with Logical Operators: You can combine multiple filters using logical operators like and, or, and not to create more complex queries. Example: Get-MgUser -Filter "jobTitle eq 'Manager' or jobTitle eq 'Director'"
  • Limit the Data Returned: Combine the -Filter parameter with -Top and -Select to limit and streamline the data returned, making your queries more efficient. Example: Get-MgUser -Filter "department eq 'Sales'" -Top 5 -Select "displayName,jobTitle"
  • Test Filters in the Graph Explorer: Use the Microsoft Graph Explorer to test and validate your filter expressions. This can help you refine your queries before using them in PowerShell scripts.
  • Stay Updated with Graph API Changes: Keep an eye on updates and changes to the Microsoft Graph API, as new properties and features might affect how you construct your filters.

By following these tips, you'll be able to create more efficient and effective queries using the -Filter parameter in Graph PowerShell, making your Microsoft 365 management tasks smoother and more productive.


⚡ Filters Run Server-Side, Not Client-Side

When you use the -Filter parameter, the query is processed directly by Microsoft Graph before data is returned. This reduces the amount of data transferred and speeds up performance compared to using Where-Object, which applies filtering locally after all data is retrieved.
🔎 Use OData Operators for Complex Queries

The -Filter parameter supports OData operators like eq, ne, startswith, and and/or. For example, you can filter users whose account is enabled and located in a specific department:

Get-MgUser -Filter "accountEnabled eq true and department eq 'HR'"
This makes it possible to perform more granular, precise queries directly within Microsoft Graph.

Suggested Reading:

Using Where-Object In Graph PowerShell
Using Powershell Graph Search Query
Using Select-Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell

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