# remove all licenses from a user in O365

**URL:** <https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362>\
**Category:** PowerShell Help\
**Created:** [May 11, 2020, 10:56am UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362 "2020-05-11T10:56:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [May 11, 2020, 10:56am UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362/1 "2020-05-11T10:56:01Z")

</div>

Hi,  
I’m trying to remove all the licenses from a given user in 0365

when I try to execute this code  
[pre]  
$csvFileLocation = “c:\temp\csv\toRemove.csv”  
$users = Import-Csv -Path $csvFileLocation  
foreach ($user in $users) {  
$upn = $user.UserPrincipalName  
(get-MsolUser -UserPrincipalName $upn).licenses.AccountSkuId |  
foreach{  
Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $\_  
}  
}[/pre]  
then I get this error

[pre]  
Set-MsolUserLicense : Unknown error occurred.  
At line:5 char:9

- foreach{Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $ …
- 

```auto

```

- CategoryInfo : OperationStopped: (🙂 [Set-MsolUserLicense], MicrosoftOnlineException
- FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.InvalidOperationOnGroupInheritedLicenseException,Microsoft.Online.Administration.Automation.Set  
UserLicense  
[/pre]

&nbsp;

how do I solve this?

&nbsp;

Paul

&nbsp;

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 11, 2020, 2:48pm UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362/2 "2020-05-11T14:48:08Z")

</div>

According to the help this should be enough:

```
$csvFileLocation = 'c:\temp\csv\toRemove.csv'
$users = Import-Csv -Path $csvFileLocation
foreach ($user in $users) {
    $Licenses = (Get-MsolUser -UserPrincipalName $user.UserPrincipalName).licenses.AccountSkuId
    Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicenses $Licenses
}
```

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [May 11, 2020, 3:31pm UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362/3 "2020-05-11T15:31:15Z")

</div>

Hi Olaf,

When I try your code I get this error

[pre]

Set-MsolUserLicense : Unknown error occurred.  
At line:5 char:5

- Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -R …
- 

```auto

```

- CategoryInfo : OperationStopped: (🙂 [Set-MsolUserLicense], MicrosoftOnlineException
- FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.InvalidOperationOnGroupInheritedLicenseException,Microsoft.Online.Administration.Automation.Set  
UserLicense

[/pre]  
which seems to be the same as mine by the looks of it

&nbsp;

Paul

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 11, 2020, 4:29pm UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362/4 "2020-05-11T16:29:11Z")

</div>

Hmmm … did you assign the licenses through a group membership or direct?

---

<div class="post-metadata">

**Author:** ![andysvints](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/andysvints/32/1017_2.png) [@andysvints](https://forums.powershell.org/u/andysvints)\
**Post date:** [May 11, 2020, 8:18pm UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362/5 "2020-05-11T20:18:19Z")

</div>

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:

> **[Error when trying to remove a license](https://techcommunity.microsoft.com/t5/office-365/error-when-trying-to-remove-a-license/m-p/308969)**
>
> Hi everyone, hoping for some help with this one.   I'm trying to remove a license from a user in my tenant: o365\_user1@.com using PowerShell.   Using Get-MsolUser I can confirm that the ENTERPRISE PACK (E3) and EMS (Enterprise Mobility +...

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [May 11, 2020, 10:23pm UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362/6 "2020-05-11T22:23:40Z")

</div>

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 you

| 
Communications Credits
 | 
Active
 | 
1/1
 | 
Inherited (Office365License-Users)
 |
| 
Enterprise Mobility + Security E5
 | 
Active
 | 
9/9
 | 
Inherited (Office365License-Users)
 |
| 
Office 365 E5
 | 
Active
 | 
36/39
 | 
Inherited (Office365License-Users)
 |

&nbsp;

or do I need to remove the licenses directly from azureAD?

&nbsp;

**Update:**

removing the license from on Prem AD sorted this out

&nbsp;

Paul

&nbsp;

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 12, 2020, 1:17am UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362/7 "2020-05-12T01:17:20Z")

</div>

> [@](#):
>
> 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_”. 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/remove-all-licenses-from-a-user-in-o365/14362/8 "2024-05-16T20:29:43Z")

</div>


