Report direct license counts
Documentation: Microsoft Learn
Get-MgUser -All requests AssignedLicenses alongside the display name.
The script
Get-MgUser -All -Property DisplayName,UserPrincipalName,LicenseAssignmentStates -ErrorAction Stop | Select-Object DisplayName, UserPrincipalName, @{Name='DirectLicenseCount'; Expression={ @($_.LicenseAssignmentStates | Where-Object { $null -eq $_.AssignedByGroup -and $_.State -in @('Active', 'ActiveWithError') } | Select-Object -ExpandProperty SkuId -Unique).Count }}Script reader
Inspect before you run
Inspect the script below. It does not run in this browser.
Inspection only — no command has run.
Show reference output
DisplayName UserPrincipalName DirectLicenseCount----------- ----------------- ------------------Sample User sample@contoso.com 1Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Get-MgUser -All -Property DisplayName,UserPrincipalName,LicenseAssignmentStates -ErrorAction Stop |
Select-Object DisplayName, UserPrincipalName,
@{Name='DirectLicenseCount'; Expression={
@($_.LicenseAssignmentStates |
Where-Object {
$null -eq $_.AssignedByGroup -and
$_.State -in @('Active', 'ActiveWithError')
} |
Select-Object -ExpandProperty SkuId -Unique).Count
}}- Stage 1Get-MgUser
-All -Property -ErrorAction - Stage 2Select-Object
Positional values or no named parameters
How it works
LicenseAssignmentStates distinguishes the origin of each SKU assignment. AssignedByGroup is null for a direct assignment. This report counts distinct directly assigned SKU identifiers whose state is Active or ActiveWithError; active-with-error assignments need investigation. Group-assigned licenses are deliberately excluded.
Adapting it
For the totals a renewal conversation needs, Get-MgSubscribedSku reports consumed and enabled units per SKU directly and skips per-user aggregation entirely.
Requirements
Microsoft.Graph module and a Graph session with approved user-read permissions such as User.Read.All. Tenant SKU totals are a separate query and may require additional permissions.
If the result is unexpected
The license-count report does not match purchased seats or group-assignment expectations.
User assigned-license counts, assignment origin and tenant subscription capacity are different measures.
- Inspect a sample user's license identifiers and assignment-state data before interpreting aggregate counts.
Validate the result
Inspect two users in the licensing view and compare their assignment origins and SKU IDs. Zero here does not mean unlicensed: a user may hold licenses assigned through groups. This counts SKU assignments, not purchased seats or enabled service plans.
Keep the check reliable
Keep the assignment-origin check when adapting the report. Produce a separate assignment-error report before interpreting active-with-error counts as healthy access.