How can I remove M365 licenses using PowerShell?

July 21, 2025 4 answers
Asked by fencing__club
Hey everyone! I've been trying to write a script to remove M365 licenses for a few days now, but I keep running into an error with the 'assignlicense' command. I'm really stuck and could use some help. If anyone has insights or suggestions, I'd love to hear them. Thanks! :)

Answers

_milk_carton
I highly recommend using AD groups for license assignments. It really streamlines the process of adding and removing licenses when you need to provision or deprovision users. Saves you a ton of manual work!
Reply to this answer
Cuppy
Make sure you're using the latest version of MS Graph PowerShell (2.29.0), as there was a bug that caused issues. Once you upgrade, you can use a script for license management that handles several actions. Check it out here: [O365 Reports](https://o365reports.com/2022/09/08/manage-365-licenses-using-ms-graph-powershell/).
Reply to this answer
8fold crease
It would help if you shared what you've tried so far. Also, this article has a lot of commands for removing licenses with PowerShell: [Microsoft Documentation](https://learn.microsoft.com/en-us/microsoft-365/enterprise/remove-licenses-from-user-accounts-with-microsoft-365-powershell?view=o365-worldwide). If you provide the full error message, we might pinpoint the issue faster!
Reply to this answer
cello rosin
You can simplify things by using Graph commands. For instance, you could run: `$License = Get-MgUserLicenseDetail -UserId ` and then `$result = Set-MgUserLicense -UserId -AddLicenses @() -RemoveLicenses $License.skuid`. Just make sure your Graph PowerShell version is up-to-date to avoid errors!
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions