Solution for Set-MgUserLicense Bug in Microsoft Graph PowerShell SDK 2.26.1
After seeing continued issues with this problem and testing extensively myself, I've confirmed this is indeed a bug in Microsoft Graph PowerShell SDK version 2.26.1. Even when following the documented syntax with -RemoveLicenses @()
, the command fails with the error:
Set-MgUserLicense : One or more parameters of the operation 'assignLicense' are missing from the request payload. The missing parameters are: removeLicenses.
Workaround Solution
Rather than downgrading to version 2.25.0 (which works but isn't always feasible), I've developed a complete solution that bypasses the broken cmdlet entirely by using direct Graph API calls through Invoke-MgGraphRequest
.
I've published a fully-functional interactive script that:
- Shows all licenses assigned to a user in a menu
- Allows selecting specific licenses to remove or all licenses at once
- Confirms changes before executing
- Verifies the remaining licenses after removal
You can find the complete script in my GitHub repository:
MgGraph-PowerShell-Tools
The Key Code That Fixes the Issue
The critical part that works around the bug is:
# Create JSON payload for license removal
$jsonBody = @{
addLicenses = @() # Must be explicitly empty
removeLicenses = $LicensesToRemove
} | ConvertTo-Json -Depth 10
# Send Graph API request to remove the licenses
Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/users/$($UserId)/assignLicense" -Body $jsonBody -ContentType "application/json"
This approach completely bypasses the problematic cmdlet and interacts directly with the Graph API endpoint, ensuring the correct payload format.
Why This Works
The bug appears to be in how the cmdlet constructs the request payload, not in the Graph API itself. By constructing the JSON payload manually and using Invoke-MgGraphRequest
, we ensure that:
- Both required parameters (
addLicenses
andremoveLicenses
) are explicitly included in the request - The proper format is maintained for the Graph API endpoint
- We avoid whatever internal issue is causing the cmdlet to fail
Using the Solution
You can use my script directly from the repository, or integrate the key code part into your existing solutions. The script works with Microsoft.Graph 2.26.1 and should continue to work even after Microsoft fixes this bug.
I've tested this solution with multiple user accounts and license types, and it reliably removes licenses where the standard cmdlet fails.