How to Use Set-MgUserLicense to Assign Microsoft 365 User License?

Managing user licenses is a core responsibility for Microsoft 365 administrators. Whether onboarding new users, updating subscriptions, or managing trial transitions, assigning licenses efficiently can save you time and reduce errors. In this blog, we’ll explore how to use the powerful Set-MgUserLicense cmdlet from Microsoft Graph PowerShell to assign Microsoft 365 licenses like a pro.

What is Set-MgUserLicense?

Set-MgUserLicense is a Microsoft Graph PowerShell cmdlet that allows you to assign or remove Microsoft 365 licenses to and from users. You can perform these actions individually or in bulk using a CSV file—making it a valuable tool for both small-scale updates and enterprise-wide operations.

Why Use Set-MgUserLicense?

Here’s why IT admins and automation pros prefer Set-MgUserLicense:

  • Streamlined License Assignment: Assign new licenses directly from PowerShell.
  • Bulk Updates: Easily manage license updates for hundreds of users using CSV input.
  • API Consistency: Ensures compatibility with Microsoft Graph API’s structure and expectations.
  • Test and Deploy: Validate changes in test environments before rolling out to production.

Cmdlet Syntax

Set-MgUserLicense -UserId <String> [-AddLicenses <IMicrosoftGraphAssignedLicense>] [-RemoveLicenses <IMicrosoftGraphGuidCollection>]

Parameters:

  • UserId: The user’s UPN (e.g., alice@domain.com) or GUID.
  • -AddLicenses: A hashtable of licenses to add (using the license SKU ID).
  • -RemoveLicenses: A list of license SKU IDs to remove.

Even if you’re not removing licenses, you must explicitly pass -RemoveLicenses @().

Usage Examples

Assign License Using UPN

Set-MgUserLicense -UserId jackie@7xh7fj.onmicrosoft.com `
-AddLicenses @{SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"} `
-RemoveLicenses @()

💡 This assigns the specified license to Jackie using her UPN. The -RemoveLicenses @() is mandatory even if no license is being removed.

Assign License Using User ID

Set-MgUserLicense -UserId cd6cd291-41ba-4b3d-ba70-5f5f07292843 `
-AddLicenses @{SkuId = "c42b9cae-ea4f-4ab7-9717-81576235ccac"} `
-RemoveLicenses @()

🔒 This is useful when you're working with GUIDs instead of UPNs, often the case in automated provisioning.

Bulk Assign User Licenses from CSV

CSV File Format

UserPrincipalName,SkuId
alice@domain.com,c42b9cae-ea4f-4ab7-9717-81576235ccac
bob@domain.com,c42b9cae-ea4f-4ab7-9717-81576235ccac

PowerShell Script

$CSVPath = "C:\Managers.csv"
$Users = Import-Csv -Path $CSVPath
                                            
foreach ($User in $Users) {
    Set-MgUserLicense -UserId $User.UserPrincipalName `
    -AddLicenses @{SkuId = $User.SkuId} `
    -RemoveLicenses @()
}
                                        

📁 Automate license assignments for your entire org from a single CSV file.

Frequently Asked Questions

  1. Where to get the SkuId for assigning licenses?
  2. Use the following command:

    Get-MgSubscribedSku | Select SkuPartNumber, SkuId

    This returns a list of all license types like ENTERPRISEPACK, BUSINESS_PREMIUM, along with their unique SkuId.

  3. Why is -RemoveLicenses @() required even when you’re not removing anything?
  4. Microsoft Graph API expects both AddLicenses and RemoveLicenses in the request body. Omitting one may result in:

    ❌ A failed request

    ⚠️ Unpredictable behavior

    Always include both parameters for a reliable execution.

  5. What permissions are required to use Set-MgUserLicense?
  6. To run Set-MgUserLicense, you must have one of the following delegated or application permissions in Microsoft Graph:

    • Delegated: User.ReadWrite.All, Directory.ReadWrite.All
    • Application: Directory.ReadWrite.All

    Whether delegated or application based permission, ensure admin consent has been granted for these permissions.

  7. What happens if the user already has a license assigned?
  8. If the user already has the specified license assigned, the cmdlet will complete without making changes. However, if you attempt to reassign the exact same license without changes (e.g., service plans within the license), it won’t affect the current configuration unless you modify the service plans specifically using a more detailed license configuration script.

Use Cases

  • User Onboarding: Automatically assign licenses as new users are added to Microsoft 365.
  • License Upgrades: Replace trial licenses with production ones during subscription transitions.
  • Bulk Licensing: Easily provision hundreds of accounts during enterprise migrations.
  • License Auditing: Standardize licensing across departments using scripted automation.

Conclusion

The Set-MgUserLicense cmdlet empowers administrators to handle Microsoft 365 licensing efficiently—from one-off assignments to full-blown bulk operations. With proper structure, permission, and a clear understanding of SkuId values, you can automate a critical part of your user lifecycle management with just a few lines of PowerShell.

🚀 Pro Tip: Always run your scripts on a few test users before applying bulk operations.

Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.

Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.

© Your Site Name. All Rights Reserved. Design by HTML Codex