This guide explores the Filter query parameter in Microsoft Graph PowerShell. Learn how to create refined queries to retrieve specific data efficiently using practical examples and tips.
The filter query parameter in Graph PowerShell is used to specify conditions for filtering the data returned from Microsoft Graph API requests. This is part of the OData query options, which allow for more precise queries by restricting the results to those that meet certain criteria.
When using PowerShell to interact with Microsoft Graph, the -Filter query can be applied in cmdlets to perform operations such as retrieving, updating, or deleting resources based on specific conditions.
The -Filter parameter typically expects a string that specifies the filter conditions. These conditions can include comparisons like equality, non-equality, greater than, and less than. Here’s the basic syntax: Get-<Resource> -Filter "Property Operator 'Value'"
To retrieve users whose job title is "Software Developer", you should pass the Software Developer value to the jobTitle -Filter query parameter of the Get-MgUser cmdlet.
To find users who are in the "HR" department and located in "New York", you should pass "HR" value to the department filter parameter and "New York" value to the city filter query parameter.
To find users with similar display names you need to pass the DisplayName parameter to the startswith function along with the alphabets of users you are searching for.
You can combine multiple conditions using AND operator. For example, you can search for active users (condition 1) in the country United States (condition 2) by executing this command.
You can use -Filter parameter with the Microsoft Graph PowerShell SDK to retrieve users from Microsoft Graph API who have a specific license assigned to them. The command you should execute is shown below. (Note: To execute the command you should know the license ID assigned. Get-MgSubscribedSKU is the cmdlet that fetches all available license IDs within your tenant. You should select the license ID you are interested in and pass it to the -Filter).
Command Breakdown
assignedLicenses/any(s: s/skuId eq 'GUID'): This part of the filter expression uses a lambda operator any. Here's what each part means:
This command is particularly useful in large organizations where administrators need to identify which users have been assigned a specific license, perhaps as part of compliance checks, licensing audits, or simply to manage license allocations more effectively. By using this command, you can quickly filter out all users who have a particular license, making it easier to manage and report on license usage within the organization.
Using the -Filter parameter effectively can help you narrow down API responses to just the relevant data, making your scripts more efficient and easier to manage.
Get-MgUser -Filter "accountEnabled eq true"
Get-MgUser -Filter "accountEnabled eq true and userType eq 'Member'"
Get-MgGroup -Filter "startsWith(displayName, 'Team')"
To further understand how -Filter query works, check out this script that implements -Filter query. The script filters for guest users and checks (and outputs) their invitation status.
Filter
Only Works on Server-Supported Properties-Filter
parameter works only on properties explicitly supported by the Microsoft Graph API for filtering.
Trying to filter on unsupported properties (like nested objects or computed fields) will result in errors such as Request_UnsupportedQuery
.
eq
, ne
, startswith
Are Commonly Used Operatorseq
(equals), ne
(not equals), and startswith
are the most commonly supported.
Refer to the Microsoft Graph OData docs for a complete list of supported expressions per entity.
© m365corner.com. All Rights Reserved. Design by HTML Codex