← script library

Automating Microsoft 365 License Management with PowerShell

Microsoft 365 & Entra IDJanuary 9, 2025

In this post, we will explore a practical script designed to automate the license management process for Microsoft 365 users. This script will help you assign licenses to users in bulk, making it easier to manage user access.

powershell
# Install the MSOnline module if its not already installed.
Install-Module -Name MSOnline -Force -AllowClobber
# Connect to Microsoft 365
$credential = Get-Credential
Connect-MsolService -Credential $credential

# Define the SKU ID for the licenses
$licenseSKU = "yourtenant:ENTERPRISEPACK"  # Replace with your actual SKU ID

# Import the user list from a CSV file
$userList = Import-Csv -Path "C:\Path\To\UserList.csv"

# Assign licenses to each user
foreach ($user in $userList) {
    try {
        Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -AddLicenses $licenseSKU
        Write-Host "License assigned to" $user.UserPrincipalName
    } catch {
        Write-Host "Failed to assign license to" $user.UserPrincipalName "Error:" $_.Exception.Message
    }
}

# Disconnect from Microsoft 365
Disconnect-MsolService

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.