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.
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.
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.
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.
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.
$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.
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.
Get-MgUser -All -Filter "startsWith(DisplayName,'Jackie')"
Lists all users whose display name starts with "Jackie".
Get-MgUser -All -Filter "endsWith(mail,'7xh7fj.onmicrosoft.com')" -Sort "displayName" -ConsistencyLevel eventual
Lists all users whose UPNs end with 7xh7fj.onmicrosoft.com.
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