Find Users Assigned Licenses with Microsoft Graph PowerShell

Learn how to identify users assigned licenses in Microsoft Entra ID using Microsoft Graph PowerShell.

# Validated on Microsoft.Graph PowerShell SDK v2.29.1
$ErrorActionPreference = 'stop'
$requiredScopes = 'User.Read.All'  

$ctx = Get-MgContext
if (-not $ctx -or ($requiredScopes | Where-Object { $ctx.Scopes -notcontains $_ })) {
    Connect-MgGraph -Scopes $requiredScopes -NoWelcome
}

$params = @{
    'All'              = $true
    'Filter'           = 'assignedLicenses/$count ne 0'
    'CountVariable'    = 'userCount'
    'ConsistencyLevel' = 'eventual'
    'PageSize'         = '999'
}

$users = Get-MgUser @params

Filtered By License

# Validated on Microsoft.Graph PowerShell SDK v2.29.1
$ErrorActionPreference = 'stop'
$requiredScopes = 'User.Read.All'  

$ctx = Get-MgContext
if (-not $ctx -or ($requiredScopes | Where-Object { $ctx.Scopes -notcontains $_ })) {
    Connect-MgGraph -Scopes $requiredScopes -NoWelcome
}

# https://learn.microsoft.com/en-us/entra/identity/users/licensing-service-plan-reference
$skuId = '05e9a617-0261-4cee-bb44-138d3ef5d965' # Microsoft 365 E3
$params = @{
    'All'              = $true
    'Filter'           = "assignedLicenses/any(x:x/skuId eq $skuId)"
    'CountVariable'    = 'userCount'
    'ConsistencyLevel' = 'eventual'
    'PageSize'         = '999'
}

$users = Get-MgUser @params

Filtered by Licenses

# Validated on Microsoft.Graph PowerShell SDK v2.29.1
$ErrorActionPreference = 'stop'
$requiredScopes = 'User.Read.All'  

$ctx = Get-MgContext
if (-not $ctx -or ($requiredScopes | Where-Object { $ctx.Scopes -notcontains $_ })) {
    Connect-MgGraph -Scopes $requiredScopes -NoWelcome
}

function Get-MgUsersWithSkuIds {
    [CmdletBinding()]
    param(
        # One or more SKU GUIDs (e.g. F3, E5)
        [Parameter(Mandatory = $true)]
        [string[]]$SkuIds,
        [string[]]$Select = @('id', 'displayName', 'userPrincipalName', 'assignedLicenses')
    )

    $clauses = $SkuIds | ForEach-Object { "assignedLicenses/any(x:x/skuId eq $_)" }
    if (-not $clauses -or $clauses.Count -eq 0) {
        throw "No SkuIds provided."
    }
    $filter = '(' + ($clauses -join ' or ') + ')'

    $params = @{
        'All'              = $true
        'Filter'           = $filter
        'Select'           = ($Select -join ',')
        'ConsistencyLevel' = 'eventual'
        'CountVariable'    = 'userCount'
        'PageSize'         = 999
    }

    $users = Get-MgUser @params

    [pscustomobject]@{
        Count = $userCount
        Query = $filter
        Users = $users
    }
}

# https://learn.microsoft.com/en-us/entra/identity/users/licensing-service-plan-reference

# ---- Usage ----
# $skuF3 = '66b55226-6b4f-492c-910c-a3b7a3c9d993'
# $skuE5 = '06ebc4ee-1bb5-47dd-8120-11324bc54e06'
# $users = Get-MgUsersWithSkuIds -SkuIds @($skuF3, $skuE5)
Loading...