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!
5 Answers
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.
I suggest you set up Active Directory groups for license assignments. It makes adding and removing licenses so much smoother.
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!
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.
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/.

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.