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.
Table of contents
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 -Force
Important: Always update to the latest Microsoft Graph PowerShell module version before you run a cmdlet or script to prevent errors and incorrect results.
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"
Get SKU ID licenses
To remove licenses to multiple users with Microsoft Graph PowerShell, you need to know the SKU ID of the license. We will use the Get-MgSubscribedSku PowerShell cmdlet to get the SkuPartNumber that shows the available licensing plans for your organization.
Get-MgSubscribedSku | Select-Object SkuPartNumber, SkuId
The PowerShell output appears.
SkuPartNumber SkuId
------------- -----
FLOW_FREE f30db892-07e9-47e9-837c-80727f46fd3d
DEVELOPERPACK_E5 c42b9cae-ea4f-4ab7-9717-81576235ccac
Find licensed users
To find all the licensed user accounts in your organization, you need to use the Get-MgUser cmdlet.
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, AssignedLicenses
The PowerShell output shows all the licensed users with their assigned licenses.
UserPrincipalName DisplayName AssignedLicenses
----------------- ----------- ----------------
Ken.Walker@m365info.com Ken Walker {c42b9cae-ea4f-4ab7-9717-81576235ccac}
Soren.Vest@m365info.com Søren Vest {c42b9cae-ea4f-4ab7-9717-81576235ccac}
Laura.Terry@m365info.com Laura Terry {c42b9cae-ea4f-4ab7-9717-81576235ccac}
Adam.Mackay@m365info.com Adam Mackay {c42b9cae-ea4f-4ab7-9717-81576235ccac}
Amanda.Hansen@m365info.com Amanda Hansen {f30db892-07e9-47e9-837c-80727f46fd3d, c42b9cae-ea4f-4ab7-9717-81576235ccac}
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 Ken Walker.
Note: You must add the -AddLicenses parameter with an empty array @() even if you want to remove licenses.
$skuid = @("c42b9cae-ea4f-4ab7-9717-81576235ccac")
Set-MgUserLicense -UserId "Ken.Walker@m365info.com" -AddLicenses @() -RemoveLicenses $skuid
The PowerShell output appears.
DisplayName Id Mail UserPrincipalName
----------- -- ---- -----------------
Ken Walker 12eefbb2-e5f4-4eec-bd18-df7ca2f1ee6b Ken.Walker@m365info.com Ken.Walker@m365info.com
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 = "Ken.Walker@m365info.com"; SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"}
@{UserId = "Amanda.Hansen@m365info.com"; SkuId = "f30db892-07e9-47e9-837c-80727f46fd3d"}
)
foreach ($user in $users) {
Set-MgUserLicense -UserId $user.UserId -AddLicenses @() -RemoveLicenses @($user.SkuId)
}
The PowerShell output results.
DisplayName Id Mail UserPrincipalName
----------- -- ---- -----------------
Ken Walker 12eefbb2-e5f4-4eec-bd18-df7ca2f1ee6b Ken.Walker@m365info.com Ken.Walker@m365info.com
Amanda Hansen 41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 Amanda.Hansen@m365info.com Amanda.Hansen@m365info.com
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 = @(
"Amanda.Hansen@m365info.com",
"Ken.Walker@m365info.com"
)
$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
}
}
The PowerShell output shows these results.
Remove license DEVELOPERPACK_E5 from Amanda Hansen
Remove license DEVELOPERPACK_E5 from Ken Walker
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
}
}
The PowerShell output shows the results.
Remove license FLOW_FREE DEVELOPERPACK_E5 from Amanda Hansen
Remove license DEVELOPERPACK_E5 from Ken Walker
No license FLOW_FREE DEVELOPERPACK_E5 assigned to Søren Vest
User Laura.Twain@m365info.com does not exist
No license DEVELOPERPACK_E5 assigned to Dean.Berg@m365info.com
That’s it!
Read more: Export Microsoft 365 Users Licenses Report »
Conclusion
You learned how to remove Microsoft 365 licenses with PowerShell. Use the Get-MgUserLicenseDetail cmdlet to retrieve each user license detail and the Set-MgUserLicense cmdlet to remove licenses. To remove specific Microsoft 365 licenses from multiple users in your organization, it’s best to use a CSV file with the PowerShell script.
Did you enjoy this article? You may also like How to Assign Microsoft 365 licenses to users and groups. Don’t forget to follow us and share this article.