Entra / Microsoft 365 · Licensing
Report license assignments to users
Report license assignments to users.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MsolService
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
If (-Not (Get-Module -Name MsOnline)) {Write-Host "Please run Connect-MSOnline before attempting to run this script"; break }$Report = [System.Collections.Generic.List[Object]]::new()$Users = Get-MsolUser -All | Where-Object {$_.isLicensed -eq $true}Write-Host "Processing Users"ForEach ($User in $Users) {$SKUs = @(Get-MsolUser -UserPrincipalName $User.UserPrincipalName | Select-Object -ExpandProperty Licenses)ForEach ($Sku in $Skus) {$Sku = $Sku.AccountSkuId.Split(":")[1]Switch ($Sku) {"EMSPREMIUM" { $License = "Enterprise Mobility & Security E5" }"ENTERPRISEPACK" { $License = "Office 365 E3" }"ENTERPRISEPREMIUM_NOPSTNCONF" { $License = "Office 365 E5 No PSTN" }"FLOW_FREE" { $License = "Power Automate" }"POWER_BI_STANDARD" { $License = "Power BI" }"RIGHTSMANAGEMENT_ADHOC" { $License = "Rights Management" }"SMB_APPS" { $License = "Business Apps" }"STREAM" { $License = "Microsoft Stream"}"WIN_DEF_ATP" { $License = "Windows Defender ATP" }default { $License = "Unknown license" }} #End Switch$ReportLine = [PSCustomObject][Ordered]@{User = $User.UserPrincipalNameSKU = $SkuLicense = $LicenseName = $User.DisplayNameTitle = $User.TitleCity = $User.CityCountry = $User.UsageLocationDepartment = $User.DepartmentCreatedOn = Get-Date($User.WhenCreated) -Format g}$Report.Add($ReportLine) }}Clear-HostWrite-Host "License information"Write-Host "-------------------"$Groupdata = $Report | Group-Object -Property License | Sort-Object Count -Descending | Select-Object Name, Count$GroupData# Set sort properties so that we get ascending sorts for one property after another$Sort1 = @{Expression='SKU'; Ascending=$true }$Sort2 = @{Expression='Name'; Ascending=$true }$Report | Select-Object SKU, Name, User | Sort-Object $Sort1, $Sort2 | Export-CSV c:\Temp\UserLicenses.CSV -NoTypeInformation
Attribution
Author
Office365itpros