Managing Microsoft 365 users manually is inefficient and prone to error. Whether you're onboarding, offboarding, licensing, or auditing users, Graph PowerShell gives you the automation edge.
Below are 8 practical, ready-to-use scripts every Microsoft 365 administrator should have.
Get essential user details including account status.
Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, AccountEnabled |
Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled
Get-MgUser -All -Property UserPrincipalName, SignInActivity |
Select-Object UserPrincipalName, @{Name="LastSignIn"; Expression={$_.SignInActivity.LastSignInDateTime}}
# Users inactive for 30+ days
$thresholdDate = (Get-Date).AddDays(-30)
$inactiveUsers = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, SignInActivity, AccountEnabled |
Where-Object {
$_.SignInActivity.LastSignInDateTime -lt $thresholdDate -and $_.AccountEnabled -eq $true
}
foreach ($user in $inactiveUsers) {
Update-MgUser -UserId $user.Id -BodyParameter @{accountEnabled = $false}
Write-Output "Disabled user: $($user.DisplayName)"
}
$UserId = "alexw@yourdomain.com"
$passwordProfile = @{
password = "TempP@ssword123!"
forceChangePasswordNextSignIn = $true
}
Update-MgUser -UserId $UserId -BodyParameter @{PasswordProfile = $passwordProfile}
Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" `
-ConsistencyLevel eventual -CountVariable Records |
Select-Object DisplayName, UserPrincipalName |
Export-Csv "LicensedUsers.csv" -NoTypeInformation
Get-MgUser -All -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" `
-ConsistencyLevel eventual -CountVariable Records |
Select-Object DisplayName, UserPrincipalName
CSV Template:
UserPrincipalName
alexw@yourdomain.com
sarahp@yourdomain.com
$users = Import-Csv "UsersToLicense.csv"
$SkuId = "your-sku-id-guid" # Get via Get-MgSubscribedSku
foreach ($user in $users) {
$UserId = $user.UserPrincipalName
Set-MgUserLicense -UserId $UserId -AddLicenses @{SkuId = $SkuId} -RemoveLicenses @{}
Write-Output "License assigned to $UserId"
}
$roles = Get-MgDirectoryRole
foreach ($role in $roles) {
Write-Host "`nRole: $($role.DisplayName)"
$members = Get-MgDirectoryRoleMember -DirectoryRoleId $role.Id
foreach ($member in $members) {
Write-Output " - $($member.AdditionalProperties.displayName) ($($member.AdditionalProperties.userPrincipalName))"
}
}
These scripts go beyond simple user listing — they help automate onboarding, security, licensing, and access management with precision and ease. Use them as-is or adapt for bulk scenarios to suit your organization’s needs.
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