48 Remove Office 365 E3 license for all users

In the below PowerShell script:-

  1. Name and save the script as “Set-remove_office365_license.PS1”.
  2. Create a folder at drive C with folder temp (C:\temp).
  3. Replace the bold red key with your o365 key that you would like to remove.
  4. Run the script now and the output will be saved as CSV form into the “C:\PS\O365\Migration” path.
In case of any hurdle, please Contact Us

 

Note: –

  1. Key needs to change at 2 places in the script. 
In case of any hurdle, please Contact Us

 

$E3 = '833ne8dd-b296-42b0-a197-1e91e994b900'
$outputs = Get-AzureADUser -Top 9000 | where {$_.AssignedLicenses.skuid -like "*$E3*" } 
$outputs.count
$outputs | sort UserPrincipalName| select MailNickName,UserPrincipalName, UsageLocation | Export-Csv -NoTypeInformation "C:\PS\O365\Migration\AllUsersE3_$((get-date).toString('yyyy-MM-dd_hh-mm-ss')).csv"




$outputsM3 = Get-AzureADUser -Top 9000 | where {$_.AssignedLicenses.skuid -like "*$($M3.skuid)*" } 
$outputsM3.count
$outputsM3 | sort UserPrincipalName| select MailNickName,UserPrincipalName, UsageLocation | Export-Csv -NoTypeInformation "C:\PS\O365\Migration\AllUsersM3_$((get-date).toString('yyyy-MM-dd_hh-mm-ss')).csv"


################################################ Remove license ############################################### .
$E3 = '833ne8dd-b296-42b0-a197-1e91e994b900'

foreach ($user in $outputs)
{
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$License.SkuId = $E3
$LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$LicensesToAssign.AddLicenses = @()
$LicensesToAssign.RemoveLicenses = $License.SkuId
Set-AzureADUserLicense -ObjectId $User.ObjectId -AssignedLicenses $LicensesToAssign
}