Remove licenses from user accounts with Office 365 PowerShell

 

Topic Last Modified: 2017-05-17

Explains how to use Office 365 PowerShell to remove Office 365 licenses that were previously assigned to users.

This section presents the procedures without fanfare or superfluous explanation. If you have questions or want more information, you can read rest of the topic.

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

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

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"

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
      
    2. Use the following syntax:

      Get-Content "<FileNameAndPath>" | Set-MsolUserLicense -UserPrincipalName $_.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" | Set-MsolUserLicense -UserPrincipalName $_.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"}

Nothing lasts forever, and that includes Office 365 licenses: sooner or later, there will come a time when you need to remove a license from a user account. Maybe the user is going on leave; maybe the user no longer needs the license; maybe – well, there are obviously any number of reasons why you might want to remove a user license.

Before we go any further it’s important to note that removing a license requires you to, well, remove the license: disabling all the services on a license is not the same thing as removing a license. For example, suppose we’ve used up all our Office 365 licenses; in other words, we have no licenses available whatsoever. You decide to follow the procedure in Disable access to services with Office 365 PowerShell to disable all the services, say, on Belinda Newman’s account. After we do that, how many licenses will we have available to us? That’s right: zero. Yes, the procedure from that topic will disable all the services on Belinda’s license, but it will not disable (i.e., delete) the license itself. The license will still be valid, and it will still be assigned to Belinda Newman. She just won’t be able to use that license to access any Office 365 services.

And that’s important: if you want to remove a license from a user you must actually remove the license. Disabling all the services will prevent the user from logging on to Office 365, but it won’t free up his or her license. If you want to take back a license that’s currently assigned to a user you need to run a command similar to this one, a command that uses the RemoveLicenses parameter to actually remove the license previously assigned to Belinda:

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

Run that command, and Belinda Newman will no longer be licensed to use Office 365.

NoteNote:
As you can see, when you use the RemoveLicenses parameter you need to specify the name of the license to be removed. If you aren’t sure which licensing plan was used to assign a license to the user just run a command like this:
Get-MsolUser -UserPrincipalName BelindaN@litwareinc.com | Format-List DisplayName,Licenses

To verify that the license really was removed, use the Get-MsolUser to check the user account in question:

Get-MsolUser -UserPrincipalName BelindaN@litwareinc.com

If everything when according to plan, Belinda’s isLicensed property will now be set to False:

UserPrincipalName            DisplayName         isLicensed
-----------------            -----------         ----------
BelindaN@litwareinc.com      Newman, Belinda     False

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.

 
Show: