How can I remove M365 licenses using PowerShell?
Asked by
83ms latency
Hey everyone! I've been trying to write a PowerShell script for days now to remove Microsoft 365 licenses, but I keep running into an error with the 'assignlicense' command. If anyone has experience with this or any tips, I would really appreciate your help. Thank you!
Answers
Looks like this might be a bug in the MS Graph PowerShell module youโre using. What version are you on? If it's not the latest (2.29.0), consider upgrading; the issue may already be resolved in that version. After updating, you can use this script to manage licenses more effectively: https://o365reports.com/2022/09/08/manage-365-licenses-using-ms-graph-powershell/.
Reply to this answer
Have you shared what youโve tried so far? It would help to see your code. Also, you can check out this Microsoft guide for the commands you need: https://learn.microsoft.com/en-us/microsoft-365/enterprise/remove-licenses-from-user-accounts-with-microsoft-365-powershell?view=o365-worldwide. Itโs a solid reference! And donโt forget to post the full error message you're gettingโit might give us more clues.
Reply to this answer
You should definitely use the Graph API for this. Hereโs a quick example to follow:
`$License = Get-MgUserLicenseDetail -UserId `
`Set-MgUserLicense -UserId -AddLicenses @() -RemoveLicenses $License.skuid`
This should help you manage your licenses better!
Reply to this answer
I suggest you set up Active Directory groups for license assignments. It makes adding and removing licenses so much smoother.
Reply to this answer
Could you share your code? Realistically, this might be exactly what group-based licensing is designed to handle, so exploring that could save you some hassle.
Reply to this answer
Add your answer
Answers are reviewed before publication.
folder+tabs: I see youโre also using MS Graph but facing issues with the Set-MgUserLicense cmdlet. That problem has been fixed in the latest version of MS Graph PowerShell. You might want to check: https://o365reports.com/2025/04/07/set-mguserlicense-operation-assign-license-missing-solution/ for more details.