Reclaiming licenses from deleted users

Brass Contributor

Hi all,

We use the sync client to import accounts from our AD. When they are imported, we automatically assign them licenses based on the group they are in. We found a couple that should not have been imported. We couldn't remove the licenses directly because they were applied based on group membership. We couldn't remove them from the group directly because of the sync. We "de-synced" them and they ended up in the Deleted Users area, but it looked like they still had licenses assigned (and ran the powershell command to verify). We deleted them from the Deleted Users to see if the licenses would be reclaimed. They have not. So, did we just lose licenses? Or does it take a while on the backend for Microsoft to let us use them again? It's been about 6 hours so far since all this went down.

 

Thanks in advance for any direction or help with head-scratching.

4 Replies

As long as you remove the account from the Deleted users container, the license should be freed.

@Laura Floeckher 

I have experienced this issue as well. Our de-provisioning process now includes removing the licenses, however some still slip through. So, I found a way to fix the issue:

All of this part is done in PowerShell while connected and logged in to the MSOLService

1. Run the command Get-MsolUser –ReturnDeletedUsers | select UserPrincipalName,IsLicensed | export-csv <Some file path where you want the file>

2. Open the file in Excel, sort by IsLicensed and delete any that are False. Save the file

3. Paste the following script in to a text editor:

Import-Csv <The file you created above> | foreach{
$UPN = $_.UserPrincipalName
Restore-MsolUser -UserPrincipalName $UPN
(get-MsolUser -UserPrincipalName $UPN).licenses.AccountSkuId |
foreach{
$License = $_
echo "Removing license: $License"
Set-MsolUserLicense -UserPrincipalName $UPN -RemoveLicenses $License
}
Remove-MsolUser -UserPrincipalName $UPN -Force
}

4. Save the script after you updated it with the file name. Make sure you end the file name with .ps1

5. Run the script in PowerShell.

 

The script reactivates the account, removes any license assigned to the account and then removes the account. You can run the command Get-MsolUser –ReturnDeletedUsers again afterwards to ensure all accounts now show False under IsLicensed.

@davidmi655 

 

This is a very nice script. I would like to add a small suggestion hoping that it would be easy to run.

 

Change in Step-1: $delUsers = Get-MsolUser -ReturnDeletedUsers | Where-Object {$_.IsLicensed -eq $true}

 

Change in Step-3: $delUsers | foreach {

 

The rest of the script in Step-3 can be used after this line. Hope this helps. Any more suggestion is welcome!

Thanks a lot !
finally, after spending 2 full days on searching for a simple and short script for bulk removal of all directly assigned to users licenses, I found this brilliant !
made some cosmetic changes, and wish to put 2 scripts here for others who may have same challenge

#Connects to your Office365 tenant
Connect-MsolService
# ============================================

# remove licences from deleted users
$delUsers = Get-MsolUser -ReturnDeletedUsers | Where-Object {$_.IsLicensed -eq $true}
$delUsers | foreach {
$UPN = $_.UserPrincipalName
Restore-MsolUser -UserPrincipalName $UPN
(get-MsolUser -UserPrincipalName $UPN).licenses.AccountSkuId | foreach {
$License = $_
echo $UPN, "Removing direct license: $License"
Set-MsolUserLicense -UserPrincipalName $UPN -RemoveLicenses $License -ErrorAction SilentlyContinue
}
Remove-MsolUser -UserPrincipalName $UPN -Force
}
# ==============================================

# remove all directly assigned licences from all *domain.com users (when you use Azure AD group targeted licensing)
$Users = Get-MsolUser -All | Where-Object {$_.IsLicensed -eq $true -and $_.UserPrincipalName -like '*domain.com'}
$Users | foreach {
$UPN = $_.UserPrincipalName
(get-MsolUser -UserPrincipalName $UPN).licenses.AccountSkuId | foreach {
$License = $_
echo $UPN, "Removing direct license: $License"
Set-MsolUserLicense -UserPrincipalName $UPN -RemoveLicenses $License -ErrorAction SilentlyContinue
}
}