Filtering Users Using Get-MgUser Cmdlet

The -Filter parameter in the Get-MgUser cmdlet allows administrators to retrieve specific users based on defined conditions. This parameter follows the OData query syntax and enables filtering on various user attributes such as DisplayName, UserPrincipalName, AssignedLicenses, and UserType. By leveraging -Filter, administrators can efficiently fetch user details without retrieving unnecessary data.

Get-MgUser -Filter Examples


1. Retrieve Only Enabled Users

To fetch all users whose accounts are enabled:

Get-MgUser -All -Filter "accountEnabled eq true"

This command retrieves all users where the accountEnabled attribute is set to true, meaning the accounts are active.

2. Fetch Guest Users Only

To retrieve all guest users in the directory:

Get-MgUser -All -Filter "userType eq 'Guest'" -ConsistencyLevel eventual

This command filters users based on their userType, listing only guest accounts.

3. Fetch Licensed Users

Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records

This command fetches and lists only the licensed users.

4. Fetch Unlicensed Users

Get-MgUser -All -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records

This command fetches and lists only the unlicensed users.

5. Filter Users Created in the Last 30 Days

$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ")
Get-MgUser -All -Filter "createdDateTime ge $startDate" -ConsistencyLevel eventual

Retrieves all Microsoft 365 users who were created in the last 30 days. Helpful for tracking new joiners or monitoring provisioning activity.

6. Filter Users Based on Country

Get-MgUser -All -Filter "country eq 'United States'" -ConsistencyLevel eventual

Lists all Microsoft 365 users whose country attribute is set to United States. Useful for region-based user management or compliance reporting.

7. Filter Users Based on Display Name

Get-MgUser -All -Filter "startsWith(DisplayName,'Jackie')"

Lists all users whose display name starts with "Jackie".

8. Filter Users Based on UserPrincipalName

Get-MgUser -All -Filter "endsWith(mail,'7xh7fj.onmicrosoft.com')" -Sort "displayName" -ConsistencyLevel eventual 

Lists all users whose UPNs end with 7xh7fj.onmicrosoft.com.

Important Considerations:

  • OData Syntax Compliance: The -Filter parameter strictly follows OData syntax, meaning operators like eq, ne, gt, lt, and functions like startswith() and endswith() must be used correctly.
  • Use of ConsistencyLevel eventual: Certain filter queries, especially those involving $count, require -ConsistencyLevel eventual to function properly.
  • Performance Optimization: Using -Filter significantly reduces the number of retrieved results, optimizing API performance and reducing unnecessary data retrieval.
  • Limitations: Not all properties are filterable. If a query fails, verify whether the property supports filtering by referring to Microsoft Graph documentation.

By effectively utilizing the -Filter parameter in Get-MgUser, administrators can streamline user retrieval, making Microsoft 365 user management more efficient.

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