← script library

PowerShell Script to Retrieve M365 User License Info

Microsoft 365 & Entra IDJanuary 9, 2025

In this post, I am excited to share a handy PowerShell script that allows you to retrieve license information for users in Microsoft 365 (M365). This script will help administrators efficiently manage their users licenses and ensure compliance across the organization.

powershell
# Install MSOnline module (if not already installed)
Install-Module -Name MSOnline -Force -AllowClobber
# Import the module and connect
Import-Module MSOnline
$credential = Get-Credential
Connect-MsolService -Credential $credential

# Retrieve users and their license details
$users = Get-MsolUser -All | Select-Object DisplayName, UserPrincipalName, Licenses
$users

# Display the formatted license information
$users | ForEach-Object {
    $licenseInfo = if ($_.Licenses.Count -gt 0) {
        ($_.Licenses | ForEach-Object { $_.AccountSkuId }) -join , 
    } else {
        "No License Assigned"
    }
    [PSCustomObject]@{
        Display Name = $_.DisplayName
        Email = $_.UserPrincipalName
        License Info = $licenseInfo
    }
} | Format-Table -AutoSize

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.