Unable to remove user license using the Set-MgUserLicense due to dependencies ?

Enterprise Architect 2,141 Reputation points

Hi All,

May I know how can I recursively or automatically remove all licenses assigned to a user using the [https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.users.actions/set-mguserlicense?view=graph-powershell-1.0 ?

Because I am stuck with this error:

Set-MgUserLicense : License assignment failed because service plan fe71d6c3-a2ea-4499-9778-da042bf08063 depends on the service plan(s) 5dbe027f-2339-4123-9542-606e4d348a72

Many thanks for considering my request.

Microsoft Graph Users API
Microsoft Graph Users API
A Microsoft API that allows you to build compelling app experiences based on users, their relationships with other users and groups, and the resources they access for example their mails, calendars, files, administrative roles, group memberships.
551 questions
Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
177 questions
Azure Active Directory
Azure Active Directory
An Azure enterprise identity service that provides single sign-on and multi-factor authentication.
12,777 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
605 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Andy David - MVP 110.7K Reputation points MVP

    Here is what I use - setting $_.Userprincipalname to the account in question:

    [array]$SkusToRemove = Get-MgUserLicenseDetail -UserId $_.Userprincipalname


    Set-MgUserLicense -UserId $_.Userprincipalname -RemoveLicenses $SkusToRemove.skuId -AddLicenses @() -Confirm:$false