🔧 New: User Management Graph PowerShell Toolkit

Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.

🚀 Launch Toolkit

Bulk Assign Microsoft 365 License Using Graph PowerShell

Assigning 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.


The Script

  1. CSV Format
  2. 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.

  3. Assign Licenses Using UserPrincipalName
  4. $CSVPath = "users.csv"
    $Users = Import-Csv -Path $CSVPath                                
    foreach ($User in $Users) {
        Set-MgUserLicense -UserId $User.UserPrincipalName `
        -AddLicenses @{SkuId = $User.SkuId} `
        -RemoveLicenses @()
    }
                                    
  5. Assign Licenses Using UserId
  6. 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 @()
    }

How the Script Works

  • Import CSV: Loads user license data from a file containing either UserPrincipalName or UserId with the desired SkuId.
  • Loop Through Users: For each user, the script:
  • Parses each log entry to extract:
    • Adds a license using the Set-MgUserLicense cmdlet.
    • Specifies the license to add using the -AddLicenses parameter.
    • Leaves the -RemoveLicenses as an empty hashtable to avoid removing existing licenses.
    • ℹ️ To fetch a user's ID if only UPN is available, use:

      Get-MgUser -UserId morrison@7xh7fj.onmicrosoft.com | Select-Object Id


Further Enhancements

  • Include License Validation:
  • Validate if a user already has the license before assigning.

  • Track Output:
  • Log successful or failed license assignments to a CSV file for auditing.

  • Skip Disabled Accounts:
  • Filter out users where AccountEnabled -eq $false.

  • Multiple SKU Assignment:
  • Modify -AddLicenses to accept more than one license:

    -AddLicenses @(@{SkuId = "sku1"}, @{SkuId = "sku2"})

Possible Errors & Solutions

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).

Use Cases

  • Employee Onboarding: Automatically assign Microsoft 365 licenses during new hire provisioning.
  • License Migration: Transition users from one SKU (e.g., Business Basic → E3).
  • Bulk Re-licensing: Reassign licenses post-renewal or tenant restructuring.
  • Scripted Compliance: Automate license assignments based on department or role.

Conclusion

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.


Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                


                


                

© m365corner.com. All Rights Reserved. Design by HTML Codex