How can I remove M365 licenses using PowerShell?

July 21, 2025 5 answers
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

faint.signal.
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
-municipal code
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
evening referendum
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!

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.

Reply to this answer
nominally_correct
I suggest you set up Active Directory groups for license assignments. It makes adding and removing licenses so much smoother.
Reply to this answer
sunflower.seedling
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.

Related Discussions