To manage Microsoft 365 licenses in your organization, you may need to remove certain licenses to control access to apps like Exchange, Teams, or OneDrive. This can be done for single or multiple users with the Set-MgUserLicense cmdlet. In this article, you will learn how to remove Microsoft 365 licenses with PowerShell.
Microsoft 365 licenses
Each organization owns different Microsoft 365 products, where a product may contain licenses from multiple subscriptions. Some users in your organization have multiple licenses, whereas others only have one license assigned.
To remove specific Microsoft 365 licenses from a single user or multiple users, it’s faster to use PowerShell. There are multiple variations in PowerShell that you can use to remove Microsoft 365 licenses from a single user or multiple users.
Install Microsoft Graph PowerShell
Before you start, you must Install the Microsoft Graph PowerShell module. Start Windows PowerShell as administrator and run the command below.
Install-Module Microsoft.Graph -ForceCode language: CSS (css)Connect to Microsoft Graph PowerShell
Then you must Connect to Microsoft Graph PowerShell with the scopes below.
Connect-MgGraph -Scopes "User.ReadWrite.All", "Organization.Read.All"Code language: CSS (css)Important:
Before you proceed, make sure you have a recent backup and verify the configuration in a test environment first.
Get SKU ID licenses
To remove licenses to multiple users with Microsoft Graph PowerShell, you need to know the SKU ID of the license.
Get-MgSubscribedSku | Select-Object SkuPartNumber, SkuIdCode language: JavaScript (javascript)The PowerShell output appears.
SkuPartNumber SkuId
------------- -----
FLOW_FREE f30yt892-07e9-47e9-837c-80727f46fd3d
DEVELOPERPACK_E5 c42b9cae-ea4f-4ab7-9717-81576235yfacFind licensed users
Get all Microsoft 365 licensed users, excluding guests, with the PowerShell command below.
Get-MgUser -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" -All -ConsistencyLevel eventual -CountVariable licensedUserCount -Select UserPrincipalName, DisplayName, AssignedLicenses | Format-Table -Property UserPrincipalName, DisplayName, AssignedLicensesCode language: JavaScript (javascript)The PowerShell output shows all the licensed users with their assigned licenses.
UserPrincipalName DisplayName AssignedLicenses
----------------- ----------- ----------------
tolga.kaya@sysarticle.com Tolga Kaya {c42b9cae-ea4f-4ab7-9717-81576235ccac}
Adem@sysarticle.com Adem {c42b9cae-ea4f-4ab7-9717-81576235ccac}
David@sysarticle.com David {c42b9cae-ea4f-4ab7-9717-81576235ccac}
Adam@sysarticle.com Adam {c42b9cae-ea4f-4ab7-9717-81576235ccac}
Arif@sysarticle.com Arif {f30db892-07e9-47e9-837c-80727f46fd3d, c42b9cae-ea4f-4ab7-9717-81576235ccac}Code language: CSS (css)Remove Microsoft 365 licenses from a single user
To remove licenses from a specific user, you will use the Set-MgUserLicense PowerShell cmdlet. In our example, we want to remove the DEVELOPERPACK_E5 license from the user Tolga Kaya.
$skuid = @("c42b9cae-ea4f-4ab7-9717-81576235ccac")
Set-MgUserLicense -UserId "[email protected]" -AddLicenses @() -RemoveLicenses $skuidCode language: PHP (php)The PowerShell output appears.
DisplayName Id Mail UserPrincipalName
----------- -- ---- -----------------
Tolga Kaya 12eefbb2-e5f4-4eec-bd18-df7ca2f1ee6b Tolga.Kaya@sysarticle.com [email protected]Code language: CSS (css)Remove Microsoft 365 licenses from multiple users
To remove different Microsoft 365 licenses from multiple users, use the PowerShell script below.
Remove different Microsoft 365 licenses from multiple users
- Type the UserPrincipalName and SkuId in line 2 for the first user
- Type the UserPrincipalName and SkuId in line 3 for the second user
- Run the PowerShell script
$users = @(
@{UserId = "[email protected]"; SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"}
@{UserId = "[email protected]"; SkuId = "f30db892-07e9-47e9-837c-80727f46fd3d"}
)
foreach ($user in $users) {
Set-MgUserLicense -UserId $user.UserId -AddLicenses @() -RemoveLicenses @($user.SkuId)
}Code language: PHP (php)The PowerShell output results.
DisplayName Id Mail UserPrincipalName
----------- -- ---- -----------------
Tolga Kaya 12eefbb2-e5f4-4eec-bd18-df7ca2f1ee6b Tolga.Kaya@sysarticle.com [email protected]
Adem 41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 [email protected] [email protected]Code language: CSS (css)Remove specific Microsoft 365 licenses from multiple users
To remove a specific Microsoft 365 license from multiple users, use the PowerShell script below.
- List the UserPrincipalName for each user from line 2
- Type the SkuPartNumber name in line 5
- Run the PowerShell script
$users = @(
"[email protected]",
"[email protected]"
)
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }
foreach ($user in $users) {
$mgUser = Get-MgUser -Filter "UserPrincipalName eq '$user'"
try {
$userLicense = Get-MgUserLicenseDetail -UserId $mgUser.Id | Where-Object { $_.SkuId -eq $license.SkuId }
if ($userLicense) {
$null = Set-MgUserLicense -UserId $mgUser.Id -AddLicenses @() -RemoveLicenses @($license.SkuId) -ErrorAction Stop
Write-Host "Remove license $($license.SkuPartNumber) from $($mgUser.DisplayName)" -ForegroundColor Green
}
else {
Write-Host "No license $($license.SkuPartNumber) assigned to $($mgUser.DisplayName)" -ForegroundColor Yellow
}
}
catch {
Write-Host "Error processing $($mgUser.DisplayName): $($_.Exception.Message)" -ForegroundColor Red
}
}Code language: PHP (php)The PowerShell output shows these results.
Remove license DEVELOPERPACK_E5 from Tolga.Kaya
Remove license DEVELOPERPACK_E5 from AdemCode language: JavaScript (javascript)Remove Microsoft 365 licenses from multiple users with CSV file
To bulk remove specific Microsoft 365 licenses from multiple users, it’s best to create a CSV file containing the users’ UPNs and SkuIDs. Then, use PowerShell script to automate the removal process based on that list.
- Type UPN as the header in the first column
- List all the users
- Type SkuID as the header in the second column
- List all the SkuIDs

- Create the folder temp if you don’t have it already in the (C:) drive
- Name the file Users.csv
- Save as type CSV (Comma delimited (*.csv)
- Click Save

- Type the CSV file path in line 1
- Run the PowerShell script below
$Users = Import-Csv "C:\temp\Users.csv"
$license = Get-MgSubscribedSku
foreach ($user in $users) {
$userId = $user.Upn
$mgUser = Get-MgUser -Filter "UserPrincipalName eq '$($user.Upn)'"
if ($mgUser) {
try {
$userLicense = Get-MgUserLicenseDetail -UserId $mgUser.Id | Where-Object { $_.SkuId -eq $license.SkuId }
if ($userLicense) {
$null = Set-MgUserLicense -UserId $mgUser.Id -AddLicenses @() -RemoveLicenses @($license.SkuId) -ErrorAction Stop
Write-Host "Remove license $($license.SkuPartNumber) from $($mgUser.DisplayName)" -ForegroundColor Green
}
else {
Write-Host "No license $($license.SkuPartNumber) assigned to $($mgUser.DisplayName)" -ForegroundColor Yellow
}
}
catch {
Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
break
}
}
else {
Write-Host "User $userId does not exist" -ForegroundColor Red
}
}Code language: PHP (php)The PowerShell output shows the results.
Remove license FLOW_FREE DEVELOPERPACK_E5 from Tolga Kaya
Remove license DEVELOPERPACK_E5 from Adem
No license FLOW_FREE DEVELOPERPACK_E5 assigned to David
User [email protected] does not exist
No license DEVELOPERPACK_E5 assigned to [email protected]Code language: JavaScript (javascript)That’s it!
Read more: Best Export Group Memberships in Microsoft Entra ID with PowerShell
via: o365info






