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 @()}