Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitIf you manage Microsoft 365 users, chances are you’ve come across the Get-MgUser cmdlet. This powerful Microsoft Graph PowerShell command helps you fetch detailed information about users in your Microsoft 365 tenant — from their names and departments to licenses, sign-in activities, and more.
In this guide, we’ll walk through what Get-MgUser does, why it’s useful, how to use it effectively, and show you real-world examples and scripts (bulk password reset and account disable scripts) illustrating Get-MgUser cmdlet usage that can make your admin life easier.
The Get-MgUser cmdlet is part of the Microsoft Graph PowerShell module, which allows administrators to interact with Microsoft 365 services using Graph API endpoints.
Simply put, it retrieves information about one or more users from your Microsoft 365 tenant. You can use it to:
The older Get-MsolUser and Get-AzureADUser cmdlets are being deprecated. Microsoft now recommends using Microsoft Graph PowerShell, where Get-MgUser serves as the new standard for retrieving user information.
Here’s why it’s worth using:
Here’s the basic syntax of the Get-MgUser cmdlet:
Get-MgUser [-UserId <String>] [-ConsistencyLevel <String>]
| Parameter | Description |
|---|---|
| -UserId | Specifies the user to retrieve, using either the User ID (GUID) or UPN (email address). |
| -ConsistencyLevel | Used for advanced queries (like $count) requiring eventual consistency. |
You can also add parameters like -All, -Top, and -Filter to refine your search.
Get-MgUser -All
Retrieves all users in the Microsoft 365 tenant.
Get-MgUser -UserId "ab05dabd-d9fb-4e2e-b9e9-fb290157a12b"
Fetches details of a single user using their unique User ID.
Get-MgUser -UserId "samadmin@7xh7fj.onmicrosoft.com"
Retrieves user details based on their User Principal Name (UPN).
Get-MgUser -Filter "Department eq 'Sales'"
Returns only users who belong to the Sales department.
Get-MgUser -All -Filter "assignedLicenses/$count ne 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records
Lists all licensed users (Members only) in your organization.
Get-MgUser -All -Filter "assignedLicenses/$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records
Retrieves all users without any assigned licenses.
Get-MgUser -Top 10
Displays only the first 10 users.
Let’s now explore two powerful real-world automation scripts where Get-MgUser makes your daily administrative tasks much easier.
Scenario:
As an administrator, you may need to reset passwords for all users periodically — for example, after a security breach or compliance audit.
Script:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Fetch all users in the tenant
$Users = Get-MgUser -All
# Loop through each user and reset password
foreach ($User in $Users) {
$NewPassword = [System.Web.Security.Membership]::GeneratePassword(12, 2)
$Params = @{
PasswordProfile = @{
ForceChangePasswordNextSignIn = $true
Password = $NewPassword
}
}
# Update user password
Update-MgUser -UserId $User.Id -BodyParameter $Params
Write-Host "Password reset for: $($User.DisplayName) | New Password: $NewPassword"
}
💡 Tip: Store the new passwords securely if you need to share them with users later.
Inactive users can be a security risk. This script disables accounts that haven’t signed in for 90 days.
Script:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Get all users with their SignInActivity property
$Users = Get-MgUser -All -Property "DisplayName,UserPrincipalName,AccountEnabled,SignInActivity"
# Set threshold date (90 days ago)
$ThresholdDate = (Get-Date).AddDays(-90)
foreach ($User in $Users) {
$LastSignIn = $User.SignInActivity.LastSignInDateTime
if ($LastSignIn -and ([datetime]$LastSignIn -lt $ThresholdDate)) {
$Params = @{
AccountEnabled = $false
}
Update-MgUser -UserId $User.Id -BodyParameter $Params
Write-Host "Disabled account: $($User.DisplayName) | Last Sign-In: $LastSignIn"
}
}
Optional: Modify $ThresholdDate to a different number of days as needed.
The Get-MgUser cmdlet is one of the most essential tools for Microsoft 365 administrators. Whether you’re fetching user data, filtering licensed accounts, or integrating it into automation scripts, it provides unmatched flexibility and depth.
By combining Get-MgUser with other Graph cmdlets like Update-MgUser, you can automate powerful workflows such as bulk password resets and account management — saving time and enhancing security.
🚀 Pro Tip: Always test scripts on a few users before running them tenant-wide, and use -WhatIf when available to preview the impact.
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