Home

Reclaiming licenses from deleted users

%3CLINGO-SUB%20id%3D%22lingo-sub-116488%22%20slang%3D%22en-US%22%3EReclaiming%20licenses%20from%20deleted%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-116488%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%3EWe%20use%20the%20sync%20client%20to%20import%20accounts%20from%20our%20AD.%20When%20they%20are%20imported%2C%20we%20automatically%20assign%20them%20licenses%20based%20on%20the%20group%20they%20are%20in.%20We%20found%20a%20couple%20that%20should%20not%20have%20been%20imported.%20We%20couldn't%20remove%20the%20licenses%20directly%20because%20they%20were%20applied%20based%20on%20group%20membership.%20We%20couldn't%20remove%20them%20from%20the%20group%20directly%20because%20of%20the%20sync.%26nbsp%3BWe%26nbsp%3B%22de-synced%22%20them%20and%20they%20ended%20up%20in%20the%20Deleted%20Users%20area%2C%20but%20it%20looked%20like%20they%20still%20had%20licenses%20assigned%20(and%20ran%20the%20powershell%20command%20to%20verify).%20We%20deleted%20them%20from%20the%20Deleted%20Users%20to%20see%20if%20the%20licenses%20would%20be%20reclaimed.%20They%20have%20not.%20So%2C%20did%20we%20just%20lose%20licenses%3F%20Or%20does%20it%20take%20a%20while%20on%20the%20backend%20for%20Microsoft%20to%20let%20us%20use%20them%20again%3F%20It's%20been%20about%206%20hours%20so%20far%20since%20all%20this%20went%20down.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%20for%20any%20direction%20or%20help%20with%20head-scratching.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-116488%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%20Center%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-564166%22%20slang%3D%22en-US%22%3ERe%3A%20Reclaiming%20licenses%20from%20deleted%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-564166%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F74962%22%20target%3D%22_blank%22%3E%40Laura%20Floeckher%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20experienced%20this%20issue%20as%20well.%20Our%20de-provisioning%20process%20now%20includes%20removing%20the%20licenses%2C%20however%20some%20still%20slip%20through.%20So%2C%20I%20found%20a%20way%20to%20fix%20the%20issue%3A%3C%2FP%3E%3CP%3EAll%20of%20this%20part%20is%20done%20in%20PowerShell%20while%20connected%20and%20logged%20in%20to%20the%20MSOLService%3C%2FP%3E%3CP%3E1.%20Run%20the%20command%20Get-MsolUser%20%E2%80%93ReturnDeletedUsers%20%7C%20select%20UserPrincipalName%2CIsLicensed%20%7C%20export-csv%20%3CSOME%20file%3D%22%22%20path%3D%22%22%20where%3D%22%22%20you%3D%22%22%20want%3D%22%22%20the%3D%22%22%20file%3D%22%22%3E%3C%2FSOME%3E%3C%2FP%3E%3CP%3E2.%20Open%20the%20file%20in%20Excel%2C%20sort%20by%20IsLicensed%20and%20delete%20any%20that%20are%20False.%20Save%20the%20file%3C%2FP%3E%3CP%3E3.%20Paste%20the%20following%20script%20in%20to%20a%20text%20editor%3A%3C%2FP%3E%3CP%3EImport-Csv%20%3CTHE%20file%3D%22%22%20you%3D%22%22%20created%3D%22%22%20above%3D%22%22%3E%20%7C%20foreach%7B%3CBR%20%2F%3E%24UPN%20%3D%20%24_.UserPrincipalName%3CBR%20%2F%3ERestore-MsolUser%20-UserPrincipalName%20%24UPN%3CBR%20%2F%3E(get-MsolUser%20-UserPrincipalName%20%24UPN).licenses.AccountSkuId%20%7C%3CBR%20%2F%3Eforeach%7B%3CBR%20%2F%3E%24License%20%3D%20%24_%3CBR%20%2F%3Eecho%20%22Removing%20license%3A%20%24License%22%3CBR%20%2F%3ESet-MsolUserLicense%20-UserPrincipalName%20%24UPN%20-RemoveLicenses%20%24License%3CBR%20%2F%3E%7D%3CBR%20%2F%3ERemove-MsolUser%20-UserPrincipalName%20%24UPN%20-Force%3CBR%20%2F%3E%7D%3C%2FTHE%3E%3C%2FP%3E%3CP%3E4.%20Save%20the%20script%20after%20you%20updated%20it%20with%20the%20file%20name.%20Make%20sure%20you%20end%20the%20file%20name%20with%20.ps1%3C%2FP%3E%3CP%3E5.%20Run%20the%20script%20in%20PowerShell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20reactivates%20the%20account%2C%20removes%20any%20license%20assigned%20to%20the%20account%20and%20then%20removes%20the%20account.%20You%20can%20run%20the%20command%20Get-MsolUser%20%E2%80%93ReturnDeletedUsers%20again%20afterwards%20to%20ensure%20all%20accounts%20now%20show%20False%20under%20IsLicensed.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-116566%22%20slang%3D%22en-US%22%3ERe%3A%20Reclaiming%20licenses%20from%20deleted%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-116566%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20long%20as%20you%20remove%20the%20account%20from%20the%20Deleted%20users%20container%2C%20the%20license%20should%20be%20freed.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-581682%22%20slang%3D%22en-US%22%3ERe%3A%20Reclaiming%20licenses%20from%20deleted%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-581682%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F341539%22%20target%3D%22_blank%22%3E%40davidmi655%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20a%20very%20nice%20script.%20I%20would%20like%20to%20add%20a%20small%20suggestion%20hoping%20that%20it%20would%20be%20easy%20to%20run.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EChange%20in%20Step-1%3A%20%24delUsers%20%3D%20Get-MsolUser%20-ReturnDeletedUsers%20%7C%20Where-Object%20%7B%24_.IsLicensed%20-eq%20%24true%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EChange%20in%20Step-3%3A%20%24delUsers%20%7C%20foreach%20%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20rest%20of%20the%20script%20in%20Step-3%20can%20be%20used%20after%20this%20line.%20Hope%20this%20helps.%20Any%20more%20suggestion%20is%20welcome!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Laura Floeckher
Occasional 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.

3 Replies
Highlighted

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

Highlighted

@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.

Highlighted

@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!