Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitAssigning Microsoft 365 licenses in bulk is a common administrative task, especially when onboarding new users or updating licensing across the organization. This article guides you through using Microsoft Graph PowerShell to efficiently assign licenses using both UserPrincipalName and UserId fields from a CSV file.
Save the following content into a file named users.csv:
UserPrincipalName,SkuId
alex.wilson@contoso.com,78e66a63-337a-4a9a-8959-41c6654dfb56
sarah.connor@contoso.com,78e66a63-337a-4a9a-8959-41c6654dfb56
📌 SkuId can be obtained by referring to the license name (identified by SKUPartNumber) you wish to assign. Use Get-MgSubscribedSku | Select SkuPartNumber, SkuId
to fetch the license SkuId and SkuPartNumber.
$CSVPath = "users.csv"
$Users = Import-Csv -Path $CSVPath
foreach ($User in $Users) {
Set-MgUserLicense -UserId $User.UserPrincipalName `
-AddLicenses @{SkuId = $User.SkuId} `
-RemoveLicenses @()
}
If your CSV uses UserId instead:
CSV Format
UserId,SkuId
4a3d5094-xxxx-xxxx-xxxx-2ab514c3b167,78e66a63-337a-4a9a-8959-41c6654dfb56
92df90ab-xxxx-xxxx-xxxx-f20aa198db8d,78e66a63-337a-4a9a-8959-41c6654dfb56
Use this script:
$CSVPath = "users.csv"
$Users = Import-Csv -Path $CSVPath
foreach ($User in $Users) {
Set-MgUserLicense -UserId $User.UserId `
-AddLicenses @{SkuId = $User.SkuId} `
-RemoveLicenses @()
}
ℹ️ To fetch a user's ID if only UPN is available, use:
Get-MgUser -UserId morrison@7xh7fj.onmicrosoft.com | Select-Object Id
Validate if a user already has the license before assigning.
Log successful or failed license assignments to a CSV file for auditing.
Filter out users where AccountEnabled -eq $false.
Modify -AddLicenses to accept more than one license:
-AddLicenses @(@{SkuId = "sku1"}, @{SkuId = "sku2"})
Error | Cause | Solution |
Cannot convert value of type 'System.Collections.Hashtable'... | Incorrect use of hashtable syntax | Ensure hashtable is passed as @{SkuId = $User.SkuId} and not in an array. |
Insufficient privileges to complete the operation | Missing permission in Graph API | Assign User.ReadWrite.All and Directory.ReadWrite.All permissions. |
License SKU not found or unavailable | Invalid or incorrect SkuId | Confirm SkuId using Get-MgSubscribedSku |
The user already has the license | License already assigned | Add logic to check existing licenses before assignment (optional). |
Using Microsoft Graph PowerShell for bulk license assignment not only saves time but also ensures consistency across your Microsoft 365 environment. With just a CSV file and a few lines of code, administrators can automate one of the most repetitive tasks in M365 management. Add enhancements and error handling for production readiness—and you're set to scale your IT operations smartly.
© m365corner.com. All Rights Reserved. Design by HTML Codex