Welcome › Forums › General PowerShell Q&A › remove all licenses from a user in O365
- This topic has 6 replies, 3 voices, and was last updated 8 months, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
May 11, 2020 at 4:26 pm #227335
Hi,
I’m trying to remove all the licenses from a given user in 0365when I try to execute this code
PowerShell123456789$csvFileLocation = “c:\temp\csv\toRemove.csv”$users = Import-Csv -Path $csvFileLocationforeach ($user in $users) {$upn = $user.UserPrincipalName(get-MsolUser -UserPrincipalName $upn).licenses.AccountSkuId |foreach{Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $_}}then I get this error
PowerShell1234567Set-MsolUserLicense : Unknown error occurred.At line:5 char:9+ foreach{Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $ …+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.InvalidOperationOnGroupInheritedLicenseException,Microsoft.Online.Administration.Automation.SetUserLicensehow do I solve this?
Paul
-
This topic was modified 8 months, 2 weeks ago by
acer460527.
-
This topic was modified 8 months, 2 weeks ago by
-
May 11, 2020 at 8:18 pm #227371
According to the help this should be enough:
PowerShell123456$csvFileLocation = 'c:\temp\csv\toRemove.csv'$users = Import-Csv -Path $csvFileLocationforeach ($user in $users) {$Licenses = (Get-MsolUser -UserPrincipalName $user.UserPrincipalName).licenses.AccountSkuIdSet-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicenses $Licenses} -
May 11, 2020 at 9:01 pm #227377
Hi Olaf,
When I try your code I get this error
PowerShell1234567Set-MsolUserLicense : Unknown error occurred.At line:5 char:5+ Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -R …+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.InvalidOperationOnGroupInheritedLicenseException,Microsoft.Online.Administration.Automation.SetUserLicensewhich seems to be the same as mine by the looks of it
Paul
-
May 11, 2020 at 9:59 pm #227392
Hmmm …. did you assign the licenses through a group membership or direct?
-
May 12, 2020 at 1:48 am #227422
Hello Paul,
What type of license you have on the tenant?
Some licenses need to be removed before the others, as they might be dependent on each other.
Please review following thread:
https://techcommunity.microsoft.com/t5/office-365/error-when-trying-to-remove-a-license/m-p/308969
-
May 12, 2020 at 3:53 am #227428
Olaf,
I know that for the Office365 the user is put in a security group in AD to be able to use office when I look up the same user in AzureAD then I see this info if this is usefull to youCommunications CreditsActive1/1Inherited (Office365License-Users)Enterprise Mobility + Security E5Active9/9Inherited (Office365License-Users)Office 365 E5Active36/39Inherited (Office365License-Users)or do I need to remove the licenses directly from azureAD?
Update:
removing the license from on Prem AD sorted this out
Paul
-
This reply was modified 8 months, 2 weeks ago by
acer460527.
-
This reply was modified 8 months, 2 weeks ago by
acer460527.
-
This reply was modified 8 months, 2 weeks ago by
-
May 12, 2020 at 6:47 am #227470
or do I need to remove the licenses directly from azureAD?
You can remove the licenses this way only when you assigned them directly. … it’s a kind of logic … when you remove the license while the according group membership is still “active” the license will “come back“. 😉
-
-
AuthorPosts
- The topic ‘remove all licenses from a user in O365’ is closed to new replies.