← script library

Automate Microsoft 365 User License Management with PowerShell

Microsoft 365 & Entra IDJanuary 9, 2025

In this post, I will be sharing a useful PowerShell script that automates the process of managing user licenses in Microsoft 365. This script can help administrators efficiently check and apply licenses to users, making the management of licenses much easier.

powershell
# Connect to Microsoft 365
Import-Module MSOnline
$credential = Get-Credential
Connect-MsolService -Credential $credential

# Fetch all users
$users = Get-MsolUser

# Check license status of each user
foreach ($user in $users) {
    $license = $user.Licenses | ForEach-Object { $_.AccountSkuId }
    Write-Host "User: $($user.UserPrincipalName) - License: $license"
}

# Assign a license to a user
$userUPN = "[email protected]"
$licenseSku = "yourtenant:STANDARDPACK"
Set-MsolUserLicense -UserPrincipalName $userUPN -AddLicenses $licenseSku
Write-Host "License assigned to $userUPN"

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.