← script library

Automate M365 User License Assignment with PowerShell

Microsoft 365 & Entra IDJanuary 9, 2025

In this post, we are sharing a handy PowerShell script that automates the assignment of licenses to Microsoft 365 users in bulk. Whether you are managing a small team or overseeing an entire organization, this script can save you time and effort while ensuring that users have the appropriate licenses for their roles.

powershell
# Install the AzureAD module if not already installed
Install-Module -Name AzureAD -Force -AllowClobber
# Connect to Azure AD
Connect-AzureAD

# Get all users without any licenses
$usersWithoutLicenses = Get-AzureADUser -All $true | Where-Object { -not $_.AssignedLicenses }
# Display the count of users without licenses
$usersWithoutLicenses.Count

# Define the License SKU ID (e.g., "ENTERPRISEPACK" for E3)
$skuId = "ENTERPRISEPACK"

# Assign the license to each user
foreach ($user in $usersWithoutLicenses) {
    Set-AzureADUserLicense -ObjectId $user.ObjectId -AddLicenses $skuId
    Write-Host "Assigned license to user: $($user.UserPrincipalName)"
}

# Check and display users with assigned licenses
$updatedUsers = Get-AzureADUser -All $true | Where-Object { $_.AssignedLicenses }
$updatedUsers | ForEach-Object { Write-Host "$($_.UserPrincipalName) has licenses assigned." }

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.