Most of the time, I manage the licensing part of Microsoft 365 manually, but from time to time, using PowerShell can be easy and clean.
To view account license and service details
First, connect to Microsoft Graph.
Connect-Graph
To list all license plans you purchased with part number and ID,
Get-MgSubscribedSku | Select-Object SkuPartNumber, SkuID
To list the services that are available in each license plan,
$allSKUs = Get-MgSubscribedSku
$allSKUs | ForEach-Object {
"Service Plan: " + $_.SkuPartNumber
$_.ServicePlans | Select-Object ServicePlanName, ServicePlanID
}
To list all license plans assigned to a specific user,
Get-MgUserLicenseDetail -UserID $userID
To list all services from all assigned license plans for a specific user,
(Get-MgUserLicenseDetail -UserID $userID -Property ServicePlans).ServicePlans
Service plans can be quite messy if you manage them manually so using PowerShell could save a lot of time and could make things a lot easier.
To assign or remove a license plan from a user account
Now you need to sign in to Microsoft Graph with some extra permission scope.
Connect-MgGraph -Scopes User.ReadWrite.All
You will also need to make sure the UsageLocation is set for the user before assigning any license plans, e.g. US for the United States and CA for Canada, etc. To find out all the accounts in your tenant that don’t have a UsageLocation value, run the command below.
Get-MgUser -Select Id,DisplayName,Mail,UserPrincipalName,UsageLocation,UserType | where { $_.UsageLocation -eq $null -and $_.UserType -eq 'Member' }
To assign a license plan to a specific user,
Set-MgUserLicense -UserID $userID -AddLicenses @{SkuID = $skuID} -RemoveLicenses @()
You can retrieve the license SKUID like this, where $partNumber is the name of the license plan, such as SPB for Business Premium, etc.
$skuID = (Get-MgSubscribedSku -All | Where-Object SkuPartNumber -eq $partNumber).SkuID
To remove a license plan from a specific user,
Set-MgUser-License -UserID $userID -RemoveLicenses @($skuID) -AddLicenses @()
To remove all license plans from a specific user,
Get-MgUserLicenseDetail -UserId $userID | ForEach-Object {Set-MgUserLicense -Userid $userID -RemoveLicenses @($_.skuid) -AddLicenses @()}
Checking for missing `UsageLocation` values before running `Set-MgUserLicense` prevents frustrating assignment failures right from day one, freeing up enough time to unwind with a Stardew Valley Crop Calculator walkthrough instead of troubleshooting tenant errors all evening.
Checking that `UsageLocation` is set prior to running `Set-MgUserLicense` is critical, as Microsoft Graph flat out rejects license assignments if that prerequisite step is skipped. Much like consulting the Popular Games Wiki walkthrough guides to clear prerequisites before tackling complex encounters, proper command sequencing prevents avoidable errors and script failures.
Using Get-MgSubscribedSku with SkuPartNumber and SkuId is a practical way to map purchased licenses before automating assignments. It would also be useful to include an example of filtering the results for a specific SKU.
I found Managing Microsoft Licenses in PowerShell and Microsoft Graph | KC’s Blog easy to follow. A brief caption under each example could clarify what it demonstrates without repeating the surrounding text.
Using Get-MgSubscribedSku with SkuPartNumber and SkuId is a practical way to map purchased licenses before automating assignments. It’s also worth noting that the Microsoft Graph connection cmdlet is Connect-MgGraph.