Managing users is a core responsibility for every Microsoft 365 administrator. Whether you need a full user list or want to find specific users based on conditions, the Get-MgUser cmdlet is your go-to tool.
In this article, weโll look at 10 simple and practical scripts using Get-MgUser that you can start using right away.
Connect to Microsoft Graph before running the scripts:
Connect-MgGraph -Scopes "User.Read.All"This is the most basic and widely used command.
Get-MgUser -All
๐ Use case
Get a complete list of users in your tenant.
You can fetch details for a single user using their UPN or User ID.
Get-MgUser -UserId "user@domain.com"
๐ Use case: Quickly check user properties like display name, mail, and more.
By default, not all properties are returned. You can explicitly request what you need.
Get-MgUser -All -Property DisplayName,UserPrincipalName,Department
๐ Use case:
Generate clean reports with only relevant fields.
Filter users based on conditions like account status.
Get-MgUser -Filter "accountEnabled eq false" -All
๐ Use case:
Identify users who are disabled but still exist in the tenant.
You can filter users based on attributes like department.
Get-MgUser -Filter "department eq 'IT'" -All
๐ Use case:
Fetch users belonging to a specific team or department.
You can filter only for licensed users using assignedLicenses property.
Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records
Use case:
Fetch only users with a valid license.
You can also filter for users without a license using assignedLicenses property.
Get-MgUser -All -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records
๐ Use case:
Fetch users without a license.
You can find out a userโs manager by combining Get-MgUserManager with Get-MgUser cmdlet.
$managerId = (Get-MgUserManager -UserId "Gilchrist@7xh7fj.onmicrosoft.com").Id
$manager = Get-MgUser -UserId $managerId
$manager | Select-Object Id, DisplayName, UserPrincipalName, Mail
๐ Use case:
Fetch manager details of a user.
You can also find out managers of users across a department by combining Get-MgUserManager with Get-MgUser.
$users = Get-MgUser -Filter "Department eq 'Sales'" -All
foreach ($user in $users) {
$managerId = (Get-MgUserManager -UserId $user.Id).Id
$manager = Get-MgUser -UserId $managerId
[PSCustomObject]@{
UserId = $user.UserPrincipalName
ManagerId = $manager.Id
ManagerDisplayName = $manager.DisplayName
ManagerUPN = $manager.UserPrincipalName
ManagerMail = $manager.Mail
}
}
๐ Use case:
Fetch user managers within a given department.
Exporting data is extremely useful for reporting and audits.
Get-MgUser -All -Property DisplayName,UserPrincipalName |
Select-Object DisplayName,UserPrincipalName |
Export-Csv "C:\Scripts\AllUsers.csv" -NoTypeInformation
๐ Use case:
Create reports for audits, HR, or management.
| Error | Cause | solution |
| Invalid filter clause | Incorrect syntax in the -Filter parameter. | Ensure proper OData format: "accountEnabled eq false" |
Yes, using OData syntax (e.g., department eq 'IT' and accountEnabled eq true).
Yes, specify them in -Property and Select-Object.
Yes, it uses Microsoft Graph, which is the recommended modern API.
Yes, with proper authentication setup.
The Get-MgUser cmdlet is one of the most important tools in your M365 PowerShell toolkit. From fetching all users to filtering specific groups, it helps you quickly access the data you need.
Start with simple commands and gradually explore filtering and exporting โ thatโs where the real power lies.
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex