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.
License management is a critical part of Microsoft 365 administration. With Set-MgUserLicense, you can:
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"
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 -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
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