Scripts & PS
How to Manage M365 Licences via PowerShell
Published 3 January 2026
·
6 min read
Managing Microsoft 365 licences via PowerShell is far more efficient than the admin centre for bulk operations - removing licences from leavers, assigning to new starters, and checking availability.
View licences
Connect-MgGraph -Scopes "User.ReadWrite.All","Organization.Read.All"
Get-MgSubscribedSku | Select SkuPartNumber, ConsumedUnits, @{N="Available";E={$_.PrepaidUnits.Enabled - $_.ConsumedUnits}}Assign a licence
$skuId = (Get-MgSubscribedSku | Where-Object SkuPartNumber -eq "SPB").SkuId
Set-MgUserLicense -UserId "user@contoso.com" -AddLicenses @{SkuId = $skuId} -RemoveLicenses @()Remove a licence
$sku = Get-MgSubscribedSku | Where-Object SkuPartNumber -eq "SPB" Set-MgUserLicense -UserId "leaver@contoso.com" -AddLicenses @() -RemoveLicenses @($sku.SkuId) # Remove ALL licences $ids = (Get-MgUserLicenseDetail -UserId "leaver@contoso.com").SkuId Set-MgUserLicense -UserId "leaver@contoso.com" -AddLicenses @() -RemoveLicenses $ids
Availability report
Get-MgSubscribedSku | ForEach-Object {
[PSCustomObject]@{
Licence = $_.SkuPartNumber
Total = $_.PrepaidUnits.Enabled
Assigned = $_.ConsumedUnits
Available = $_.PrepaidUnits.Enabled - $_.ConsumedUnits
}
} | Format-Table -AutoSizeFrequently Asked Questions
Q: Why does assigning a licence fail with UsageLocation error?
Every user must have UsageLocation set before a licence can be assigned. Update-MgUser -UserId "user@domain.com" -UsageLocation "GB"
Q: Can I use group-based licensing?
Yes. Entra ID P1 supports group-based licensing - assign a licence to a group and all members get it automatically.
Q: What happens to user data when I remove a licence?
Data is retained for 30 days by default. Consider putting the account in litigation hold if longer retention is needed.