Managing Microsoft 365 licenses efficiently is crucial for IT administrators. The Set-MgUserLicense cmdlet from the Microsoft Graph PowerShell module allows you to assign or remove licenses from users in a streamlined manner. This article walks you through the cmdlet's syntax, practical examples, pro tips, use cases, common errors, and their solutions.
Set-MgUserLicense -UserId [-AddLicenses <IMicrosoftGraphAssignedLicense>] [-RemoveLicenses <IMicrosoftGraphGuidCollection>]
Parameters:
Set-MgUserLicense -UserId jackie@7xh7fj.onmicrosoft.com ` -AddLicenses @{SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"} `-RemoveLicenses @()
Set-MgUserLicense -UserId cd6cd291-41ba-4b3d-ba70-5f5f07292843 ` -AddLicenses @{SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"} `-RemoveLicenses @()
CSV Format (Users.csv):
UserPrincipalName,SkuId
alice@domain.com,c42b9cae-ea4f-4ab7-9717-81576235ccac
bob@domain.com,c42b9cae-ea4f-4ab7-9717-81576235ccac
Import-Csv "Users.csv" | ForEach-Object {
Set-MgUserLicense -UserId $_.UserPrincipalName `-AddLicenses @{SkuId = $_.SkuId} `-RemoveLicenses @()}
Get-MgSubscribedSku | Select SkuPartNumber, SkuId
Yes, you can pass multiple SkuId values in the AddLicenses array within the -BodyParameter hashtable. This allows bulk license assignment in one call.
You can retrieve available SkuId values using the Get-MgSubscribedSku cmdlet. The SkuId is a GUID associated with each licensed product.
While technically possible, most Microsoft 365 licenses are intended for member users. Assigning licenses to guests may result in compliance or billing issues and is generally not recommended.
| Error | Cause | Solution |
| Cannot convert the literal 'System.Collections.Hashtable' to the expected type 'Edm.Guid'. | Incorrect use of -AddLicenses format. | Use the correct hashtable syntax: -AddLicenses @{SkuId = " |
| The property 'RemoveLicenses' must be specified for this request. | Missing -RemoveLicenses parameter. | Always include -RemoveLicenses @() even if you're not removing any licenses. |
| License assignment failed. License limit exceeded, or the user is already licensed. | The license quota has been exceeded or the user already has the license. | Check the user’s current licenses using Get-MgUserLicenseDetail and validate availability. |
| Access Denied or Insufficient privileges to complete the operation. | Lack of required permissions. | Ensure the account has User.ReadWrite.All and Directory.ReadWrite.All delegated/app consent. |
Use the following command:
Get-MgSubscribedSku | Select SkuPartNumber, SkuId
This returns a list of all license types (like ENTERPRISEPACK, BUSINESS_PREMIUM) and their unique identifiers.
The Microsoft Graph API expects both AddLicenses and RemoveLicenses to be explicitly defined in the request. Omitting one can lead to failure or unexpected behavior. Always include both for a successful operation.
Get-MgSubscribedSku to Retrieve Accurate License SKUsSet-MgUserLicense, run Get-MgSubscribedSku to get the correct SkuId values for your tenant.Set-MgUserLicense call by using both AddLicenses and RemoveLicenses in the -BodyParameter block.Set-MgUserLicense Requires -RemoveLicenses to Be Explicitly Defined-RemoveLicenses parameter must still be included.@{} if you're not removing any licenses. Omitting it can cause errors or unexpected behavior.
The Set-MgUserLicense cmdlet is a powerful and reliable way to assign or remove Microsoft 365 licenses from users. Whether you're managing a single user or deploying licenses in bulk, this cmdlet simplifies the task with consistency and automation support. Just ensure:
With proper usage, you can automate onboarding, streamline license distribution, and maintain better control over your Microsoft 365 environment.
© m365corner.com. All Rights Reserved. Design by HTML Codex