đź”§ 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

Assign License to Multiple Users Using Graph PowerShell by Importing Them From CSV File

Managing licenses for multiple users manually in the Microsoft 365 Admin Center is tedious and error-prone. This script offers a straightforward Graph PowerShell solution to automate license assignment to multiple users by importing them from a CSV file—saving both time and administrative effort.


The Script

# Load users from CSV
$users = Import-Csv "C:\Path\To\users.csv"
                                
# Get the license SKU
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "ENTERPRISEPACK" }
                                
foreach ($user in $users) {
    try {
        $userId = (Get-MgUser -UserId $user.UserPrincipalName).Id
                                        
        Set-MgUserLicense -UserId $userId `
        -AddLicenses @(@{SkuId = $license.SkuId}) `
        -RemoveLicenses @()
                                        
        Write-Host "âś… License assigned to $($user.UserPrincipalName)"
    }
    catch {
        Write-Warning "⚠️ Failed for $($user.UserPrincipalName): $_"
    }
}
                            

How the Script Works

  1. Imports the user list from a CSV file using Import-Csv. The CSV must include a column named UserPrincipalName.
  2. Retrieves the license SKU using the Get-MgSubscribedSku cmdlet, filtered by SkuPartNumber.
  3. 📝 To find the available SkuPartNumber values in your tenant, run:

    Get-MgSubscribedSku | Select-Object SkuId, SkuPartNumber

  4. Loops through each user in the CSV:
    • Retrieves the user’s ID using Get-MgUser.
    • Assigns the license using Set-MgUserLicense.
    • No licenses are removed in this script (empty -RemoveLicenses @()).
  5. Outputs results for each user—showing success ✅ or failure ⚠️.

Further Enhancements

  • Bulk Remove a License: Modify the -RemoveLicenses array to remove a specific license.
  • Assign Different Licenses per User: Expand the CSV to include a LicenseSku column.
  • Add Logging: Export results (success/failure) to a separate CSV for audit purposes.
  • Add Error Categorization: Log specific error messages for better troubleshooting.

Possible Errors & Solutions

Error Cause Solution
User not found Incorrect UPN in the CSV Ensure the UserPrincipalName matches exactly
Access denied Missing Graph permission scopes Connect with User.ReadWrite.All and Directory.ReadWrite.All
License assignment failed SKU not available or out of licenses Check with Get-MgSubscribedSku and verify seat availability

Conclusion

This script greatly simplifies the process of assigning licenses to multiple users. Instead of manually assigning licenses in the admin portal, you can now handle bulk assignments with one script and a properly formatted CSV file.

Ideal for onboarding scenarios or tenant-wide license transitions, this Graph PowerShell approach reduces manual overhead and brings consistency to your license management process.


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