Microsoft Entra ID · PowerShell 7.6
Disabled users who still have licences
Which disabled user accounts still have Microsoft 365 or other licences assigned, and are those licences assigned directly or through a group?
Script
#Requires -Modules Microsoft.Graph.Authentication, Microsoft.Graph.Users, Microsoft.Graph.Identity.DirectoryManagement
# Disabled user accounts that still have licences assigned. Read-only.
Connect-MgGraph -Scopes 'User.Read.All', 'LicenseAssignment.Read.All' -NoWelcome
$skuNames = @{}
Get-MgSubscribedSku -All | ForEach-Object { $skuNames[$_.SkuId] = $_.SkuPartNumber }
Get-MgUser -All -Filter 'accountEnabled eq false' -Property 'id', 'displayName', 'userPrincipalName', 'assignedLicenses', 'licenseAssignmentStates', 'onPremisesSyncEnabled' |
Where-Object { $_.AssignedLicenses.Count -gt 0 } |
ForEach-Object {
$licences = $_.AssignedLicenses | ForEach-Object {
if ($skuNames.ContainsKey($_.SkuId)) { $skuNames[$_.SkuId] } else { $_.SkuId }
}
[pscustomobject]@{
UserPrincipalName = $_.UserPrincipalName
DisplayName = $_.DisplayName
Licences = $licences -join ', '
ViaGroup = @($_.LicenseAssignmentStates | Where-Object { $_.AssignedByGroup }).Count -gt 0
SyncedFromAD = $_.OnPremisesSyncEnabled -eq $true
}
} |
Sort-Object UserPrincipalName
Checked automatically against Microsoft's published cmdlet reference. KBY hasn't run it in your tenant; run it in a test environment first.
Reading the output
- assignedLicenses includes licences inherited from groups. ViaGroup is True when at least one licence comes from group-based licensing; those have to be removed by taking the user out of the group (or changing the group's licences), not from the user.
- Licences are shown by SKU part number (for example SPE_E3). Microsoft's product names and service plan identifiers page maps part numbers to product names.
- Accounts synced from on-premises Active Directory are marked SyncedFromAD; disabling or deleting them happens in AD.
Sources
The Microsoft reference page for every cmdlet the script calls, plus the API and product pages behind each permission and caveat.
Connect-MgGraph (Microsoft.Graph.Authentication)Get-MgUser (Microsoft.Graph.Users)Get-MgSubscribedSku (Microsoft.Graph.Identity.DirectoryManagement)user resource type: accountEnabled, assignedLicenses, licenseAssignmentStateslicenseAssignmentState resource type: assignedByGroupList subscribedSkus: permissionsProduct names and service plan identifiers for licensingForEach-Object (Microsoft.PowerShell.Core)Where-Object (Microsoft.PowerShell.Core)Sort-Object (Microsoft.PowerShell.Utility)