Monitor Microsoft 365 User Licenses with PowerShell

In this post, we will share a practical PowerShell script to monitor Microsoft 365 user licenses in your organization. Keeping track of user licenses is crucial for effective resource management and cost control. This script will provide you with a detailed overview of the licenses assigned to each user, helping you identify any discrepancies or unused licenses that can be optimized.
Here is the PowerShell script to monitor Microsoft 365 user licenses:

# Connect to Microsoft 365
$credential = Get-Credential
Connect-MsolService -Credential $credential
# Get all users and their license status
$users = Get-MsolUser -All | Select-Object DisplayName, EmailAddress, isLicensed, Licenses
# Generate a report of user licenses
$licenseReport = foreach ($user in $users) {
    [PSCustomObject]@{
        DisplayName = $user.DisplayName
        Email       = $user.EmailAddress
        IsLicensed  = $user.isLicensed
        Licenses    = ($user.Licenses | ForEach-Object { $_.SkuPartNumber }) -join ", "
    }
}
# Output the report
$licenseReport | Format-Table -AutoSize
Write-Host "License report generated successfully."