Remove licenses from user accounts with Office 365 PowerShell

Use the Azure Active Directory PowerShell for Graph module

First, connect to your Office 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.AssignedLicense
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$license.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $planName -EQ).SkuID
$licenses.AddLicenses = $license
Set-AzureADUserLicense -ObjectId $userUPN -AssignedLicenses $licenses
$Licenses.AddLicenses = @()
$Licenses.RemoveLicenses =  (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $planName -EQ).SkuID
Set-AzureADUserLicense -ObjectId $userUPN -AssignedLicenses $licenses

Use the Microsoft Azure Active Directory Module for Windows PowerShell

First, connect to your Office 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 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:
$x = Get-MsolUser -All <FilterableAttributes> | where {$_.isLicensed -eq $true}
$x | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "<AccountSkuId1>", "<AccountSkuId2>"...}

This example removes the litwareinc:ENTERPRISEPACK (Office 365 Enterprise E3) licenses from all accounts for users in the Sales department in the United States.

$USSales = Get-MsolUser -All -Department "Sales" -UsageLocation "US" | where {$_.isLicensed -eq $true}
$USSales | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "litwareinc:ENTERPRISEPACK"}
  • Use a list of specific accounts 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:
Get-Content "<FileNameAndPath>" | ForEach { Set-MsolUserLicense -UserPrincipalName $_ -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.

Get-Content "C:\My Documents\Accounts.txt" | ForEach { Set-MsolUserLicense -UserPrincipalName $_ -RemoveLicenses "litwareinc:ENTERPRISEPACK" }

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

$x = Get-MsolUser -All  | Where {$_.isLicensed -eq $true}
$x | ForEach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "<AccountSkuId1>", "<AccountSkuId2>"...}

This example removes the litwareinc:ENTERPRISEPACK (Office 365 Enterprise E3) license from all existing licensed user accounts.

$x = Get-MsolUser -All  | Where {$_.isLicensed -eq $true}
$x | ForEach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "litwareinc:ENTERPRISEPACK"}

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

See also

Manage user accounts and licenses with Office 365 PowerShell

Manage Office 365 with Office 365 PowerShell

Getting started with Office 365 PowerShell

New to Office 365?

Tip

LinkedIn logo New to Office 365?

Discover free video courses for Office 365 admins and IT pros, brought to you by LinkedIn Learning.