Set-MgUserLicense

What is Set-MgUserLicense?

The Set-MgUserLicense cmdlet in Microsoft Graph PowerShell allows administrators to assign or remove Microsoft 365 licenses for users. It accepts either a User Principal Name (UPN) or a User ID as input and supports automation for individual or bulk license assignments.


Why Use Set-MgUserLicense?

License management is a critical part of Microsoft 365 administration. With Set-MgUserLicense, you can:

  • Automate license assignments for new users during onboarding.
  • Bulk assign or remove licenses across multiple accounts.
  • Ensure compliance by revoking unused or incorrect licenses.
  • Save time compared to manually applying licenses in the Microsoft 365 admin center.

Prerequisites

Before running this cmdlet, install the Microsoft Graph module and connect with proper permissions:

Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.ReadWrite.All"

How to Use Set-MgUserLicense?

Syntax (essential parameters):

Set-MgUserLicense -UserId <String> -AddLicenses <Hashtable> -RemoveLicenses <Hashtable>

You can add new licenses using -AddLicenses and remove existing ones with -RemoveLicenses.


Set-MgUserLicense Examples

Assign License Using UPN

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

Assign License Using User Id

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

Bulk Assign User License from CSV

CSV Format (Users.csv):

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

PowerShell Script for assigning licenses:

Import-Csv "Users.csv" | ForEach-Object {
Set-MgUserLicense -UserId $_.UserPrincipalName `
    -AddLicenses @{SkuId = $_.SkuId} `
    -RemoveLicenses @()
}
Note: License SkuId can be obtained executing Get-MgSubscribedSku command.

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