PowerShell Genie

Report direct license counts

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Get-MgUser -All requests AssignedLicenses alongside the display name.

The script

1Get-MgUser -All -Property DisplayName,UserPrincipalName,LicenseAssignmentStates -ErrorAction Stop |
2 Select-Object DisplayName, UserPrincipalName,
3 @{Name='DirectLicenseCount'; Expression={
4 @($_.LicenseAssignmentStates |
5 Where-Object {
6 $null -eq $_.AssignedByGroup -and
7 $_.State -in @('Active', 'ActiveWithError')
8 } |
9 Select-Object -ExpandProperty SkuId -Unique).Count
10 }}

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1DisplayName UserPrincipalName DirectLicenseCount
    2----------- ----------------- ------------------
    3Sample User sample@contoso.com 1
    Inspect 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 }}
    1. Stage 1Get-MgUser-All -Property -ErrorAction
    2. Stage 2Select-ObjectPositional 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.

      1. 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.

      Primary documentation

      Microsoft Graph: license assignment state

      Continue
      Genie guideReport direct license counts

      Get-MgUser -All requests AssignedLicenses alongside the display name.