Automatically Reclaim All Licenses from Disabled Accounts
In Microsoft 365 environments, disabled user accounts often continue consuming valuable licenses. Over time, this leads to unnecessary costs and inefficient license utilization. When employees leave the organization or accounts are disabled for compliance reasons, licenses should be reclaimed immediately.
Instead of manually reviewing each account in the admin center, this guide demonstrates how to automatically:
This script works dynamically. FLOW_FREE, DEVELOPERPACK_E5 and Power_Pages_vTrial_for_Makers licenses have been used for demo purpose:
It will reclaim all assigned licenses from disabled users — regardless of SKU type.
Try the M365Corner Microsoft 365 Reporting Tool — your DIY pack with 20+ out-of-the-box M365 reports for Users, Groups, and Teams.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All
Write-Host "Fetching disabled users..." -ForegroundColor Cyan
# Get all disabled users with assigned licenses
$DisabledUsers = Get-MgUser -Filter "accountEnabled eq false" -All -Property Id,UserPrincipalName,AssignedLicenses
if (-not $DisabledUsers) {
Write-Host "No disabled users found." -ForegroundColor Yellow
break
}
$Results = @()
foreach ($User in $DisabledUsers) {
try {
# Get assigned license SKUs
$AssignedSkuIds = $User.AssignedLicenses.SkuId
if (-not $AssignedSkuIds) {
Write-Host "$($User.UserPrincipalName) has no assigned licenses. Skipping." -ForegroundColor Yellow
$Results += [PSCustomObject]@{
UserPrincipalName = $User.UserPrincipalName
Status = "Skipped - No Licenses"
Timestamp = (Get-Date)
}
continue
}
# Remove all assigned licenses
Set-MgUserLicense -UserId $User.Id `
-AddLicenses @() `
-RemoveLicenses $AssignedSkuIds
Write-Host "Licenses reclaimed from $($User.UserPrincipalName)" -ForegroundColor Green
$Results += [PSCustomObject]@{
UserPrincipalName = $User.UserPrincipalName
LicensesRemoved = ($AssignedSkuIds -join ", ")
Status = "Success"
Timestamp = (Get-Date)
}
}
catch {
Write-Host "Failed for $($User.UserPrincipalName)" -ForegroundColor Red
Write-Host $_.Exception.Message
$Results += [PSCustomObject]@{
UserPrincipalName = $User.UserPrincipalName
Status = "Failed"
ErrorMessage = $_.Exception.Message
Timestamp = (Get-Date)
}
}
}
# Export Report
$ReportPath = "C:\Path\DisabledUserLicenseReclaimReport.csv"
$Results | Export-Csv $ReportPath -NoTypeInformation
Write-Host "Report exported to $ReportPath" -ForegroundColor Cyan
Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All
Required:
Get-MgUser -Filter "accountEnabled eq false"
This retrieves only users where:
AccountEnabled = False
Only disabled accounts are processed.
$User.AssignedLicenses.SkuId
If a user has no licenses, the script:
Set-MgUserLicense `
-AddLicenses @() `
-RemoveLicenses $AssignedSkuIds
All assigned SKUs are removed in one operation.
Important: -AddLicenses @() Is Mandatory
Even when you are not adding licenses, this parameter must be included.
If omitted, you may encounter:
Cannot convert the literal 'System.Collections.Hashtable' to the expected type 'Edm.Guid'
Always include:
-AddLicenses @()
The script exports:
DisabledUserLicenseReclaimReport.csv
Report includes:
This provides a compliance-ready audit trail.
This foundational script can be extended to:
These can each become dedicated M365Corner articles.
| Error | Cause | Solution |
|---|---|---|
| Insufficient Privileges | Missing Graph scopes or role permissions | Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All Ensure you are Global Admin or License Admin. |
| No Disabled Users Found | No accounts with AccountEnabled = False | Get-MgUser -All | Select UserPrincipalName,AccountEnabled |
| License Removal Fails | Transient Graph issue or permission boundary | Re-run script after verifying permissions. |
Disabled accounts consuming licenses lead to: Wasted subscription capacity, Increased operational cost and Poor license governance. This script transforms license management from: Manual Review → Automated Reclamation → Reported Governance. It is ideal for:
For growing Microsoft 365 tenants, automated license reclamation is not optional — it is essential.
© Created and Maintained by LEARNIT WELL SOLUTIONS. All Rights Reserved.