Removing O365 licenses in bulk via PowerShell

Removing licenses from users via the O365 Admin Center is simple and easy but for bulk operations, there is nothing better than PowerShell.

The O365 Admin Center also has a limitation of 20 users. It does not let you unassign licenses for more than 20 users at a time.

This post will cover O365 license removals from users in bulk.

Get the user list in a CSV file. Make sure it includes the User Principal Name or UPN.
In this example, we will removing the DEVELOPERPACK_E5 license from the users.

Open up PowerShell and connect to Azure AD using the commandlet Connect-AzureAD. Run the following commands to remove the licenses.

You will need to know the friendly name of the license you want to remove. Check out this post if you need to know how to get the friendly name (SKU Part Number) for the license you want to remove.

$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

$userlist = Import-Csv -Path C:\Temp\UserList.csv
ForEach ($User in $userlist) {
  Write-Host("Processing: " + $User.UserPrincipalName + ".")
$Licenses.RemoveLicenses =  (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $planName -EQ).SkuID
Set-AzureADUserLicense -ObjectId $User.UserPrincipalName -AssignedLicenses $licenses

The output should look like this.

Leave a Reply

Your email address will not be published. Required fields are marked *