Remove Microsoft 365 licenses from user accounts with PowerShell

This article applies to both Microsoft 365 Enterprise and Office 365 Enterprise.

Note

Learn how to remove licenses from user accounts with the Microsoft 365 admin center. For a list of additional resources, see Manage users and groups.

Use the Azure Active Directory PowerShell for Graph module

First, connect to your Microsoft 365 tenant.

Next, list the license plans for your tenant with this command.

Get-AzureADSubscribedSku | Select SkuPartNumber

Next, get the sign-in name of the account for which you want remove a license, also known as the user principal name (UPN).

Finally, specify the user sign-in and license plan names, remove the "<" and ">" characters, and run these commands.

$userUPN="<user sign-in name (UPN)>"
$planName="<license plan name from the list of license plans>"
$license = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$License.RemoveLicenses = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $planName -EQ).SkuID
Set-AzureADUserLicense -ObjectId $userUPN -AssignedLicenses $license

To remove all of the licenses for a specific user account, specify the user sign-in name, remove the "<" and ">" characters, and run these commands.

$userUPN="<user sign-in name (UPN)>"
$userList = Get-AzureADUser -ObjectID $userUPN
$Skus = $userList | Select -ExpandProperty AssignedLicenses | Select SkuID
if($userList.Count -ne 0) {
    if($Skus -is [array])
    {
        $licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
        for ($i=0; $i -lt $Skus.Count; $i++) {
            $Licenses.RemoveLicenses +=  (Get-AzureADSubscribedSku | Where-Object -Property SkuID -Value $Skus[$i].SkuId -EQ).SkuID   
        }
        Set-AzureADUserLicense -ObjectId $userUPN -AssignedLicenses $licenses
    } else {
        $licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
        $Licenses.RemoveLicenses =  (Get-AzureADSubscribedSku | Where-Object -Property SkuID -Value $Skus.SkuId -EQ).SkuID
        Set-AzureADUserLicense -ObjectId $userUPN -AssignedLicenses $licenses
    }
}

Use the Microsoft Azure Active Directory Module for Windows PowerShell

First, connect to your Microsoft 365 tenant.

To view the licensing plan (AccountSkuID) information in your organization, see the following topics:

If you use the Get-MsolUser cmdlet without using the -All parameter, only the first 500 accounts are returned.

Removing licenses from user accounts

To remove licenses from an existing user account, use the following syntax:

Set-MsolUserLicense -UserPrincipalName <Account> -RemoveLicenses "<AccountSkuId1>", "<AccountSkuId2>"...

Note

PowerShell Core does not support the Microsoft Azure Active Directory Module for Windows PowerShell module and cmdlets with Msol in their name. To continue using these cmdlets, you must run them from Windows PowerShell.

This example removes the litwareinc:ENTERPRISEPACK (Office 365 Enterprise E3) license from the user account BelindaN@litwareinc.com.

Set-MsolUserLicense -UserPrincipalName belindan@litwareinc.com -RemoveLicenses "litwareinc:ENTERPRISEPACK"

Note

You cannot use the Set-MsolUserLicense cmdlet to unassign users from canceled licenses. You must do this individually for each user account in the Microsoft 365 admin center.

To remove all licenses from a group of existing licensed users, use either of the following methods:

  • Filter the accounts based on an existing account attribute To do this, use the following syntax:
$userArray = Get-MsolUser -All <FilterableAttributes> | where {$_.isLicensed -eq $true}
for ($i=0; $i -lt $userArray.Count; $i++)
{
Set-MsolUserLicense -UserPrincipalName $userArray[$i].UserPrincipalName -RemoveLicenses $userArray[$i].licenses.accountskuid
}

This example removes all licenses from all user accounts in the Sales department in the United States.

$userArray = Get-MsolUser -All -Department "Sales" -UsageLocation "US" | where {$_.isLicensed -eq $true}
for ($i=0; $i -lt $userArray.Count; $i++)
{
Set-MsolUserLicense -UserPrincipalName $userArray[$i].UserPrincipalName -RemoveLicenses $userArray[$i].licenses.accountskuid
}
  • Use a list of specific accounts for a specific license To do this, perform the following steps:
  1. Create and save a text file that contains one account on each line like this:
akol@contoso.com
tjohnston@contoso.com
kakers@contoso.com
  1. Use the following syntax:
$x=Get-Content "<FileNameAndPath>"
for ($i=0; $i -lt $x.Count; $i++)
{
Set-MsolUserLicense -UserPrincipalName $x[$i] -RemoveLicenses "<AccountSkuId1>","<AccountSkuId2>"...
}

This example removes the litwareinc:ENTERPRISEPACK (Office 365 Enterprise E3) license from the user accounts defined in the text file C:\My Documents\Accounts.txt.

$x=Get-Content "C:\My Documents\Accounts.txt"
for ($i=0; $i -lt $x.Count; $i++)
{
Set-MsolUserLicense -UserPrincipalName $x[$i] -RemoveLicenses "litwareinc:ENTERPRISEPACK"
}

To remove all licenses from all existing user accounts, use the following syntax:

$userArray = Get-MsolUser -All | where {$_.isLicensed -eq $true}
for ($i=0; $i -lt $userArray.Count; $i++)
{
Set-MsolUserLicense -UserPrincipalName $userArray[$i].UserPrincipalName -RemoveLicenses $userArray[$i].licenses.accountskuid
}

Another way to free up a license is by deleting the user account. For more information, see Delete and restore user accounts with PowerShell.

See also

Manage Microsoft 365 user accounts, licenses, and groups with PowerShell

Manage Microsoft 365 with PowerShell

Getting started with PowerShell for Microsoft 365