View licensed and unlicensed Microsoft 365 users with PowerShell
This article applies to both Microsoft 365 Enterprise and Office 365 Enterprise.
User accounts in your Microsoft 365 organization may have some, all, or none of the available licenses assigned to them from the licensing plans that are available in your organization. You can use PowerShell for Microsoft 365 to quickly find the licensed and unlicensed users in your organization.
Use the Microsoft Graph PowerShell SDK
First, connect to your Microsoft 365 tenant.
Reading user properties including license details requires the User.Read.All permission scope or one of the other permissions listed in the 'Get a user' Graph API reference page.
The Organization.Read.All permission scope is required to read the licenses available in the tenant.
Connect-Graph -Scopes User.Read.All, Organization.Read.All
To view the license details of a specific user account, run the following command:
Get-MgUserLicenseDetail -UserId "<user sign-in name (UPN)>"
For example:
Get-MgUserLicenseDetail -UserId "belindan@litwareinc.com"
To view the list of all user accounts in your organization that have NOT been assigned any of your licensing plans (unlicensed users), run the following command:
Get-MgUser -Filter 'assignedLicenses/$count eq 0' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All
Write-Host "Found $unlicensedUserCount unlicensed users."
To view the list of all member user accounts (excluding guests) in your organization that have NOT been assigned any of your licensing plans (unlicensed users), run the following command:
Get-MgUser -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All
Write-Host "Found $unlicensedUserCount unlicensed users (excluding guests)."
To view the list of all user accounts in your organization that have been assigned any of your licensing plans (licensed users), run the following command:
Get-MgUser -Filter 'assignedLicenses/$count ne 0' -ConsistencyLevel eventual -CountVariable licensedUserCount -All -Select UserPrincipalName,DisplayName,AssignedLicenses | Format-Table -Property UserPrincipalName,DisplayName,AssignedLicenses
Write-Host "Found $licensedUserCount licensed users."
To view the list of all user accounts in your organization that have an E5 license assigned, run the following command:
$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
Get-MgUser -Filter "assignedLicenses/any(x:x/skuId eq $($e5sku.SkuId) )" -ConsistencyLevel eventual -CountVariable e5licensedUserCount -All
Write-Host "Found $e5licensedUserCount E5 licensed users."
Use the Azure Active Directory PowerShell for Graph module
First, connect to your Microsoft 365 tenant.
To view the list of all user accounts in your organization that have NOT been assigned any of your licensing plans (unlicensed users), run the following command:
Get-AzureAdUser | ForEach{ $licensed=$False ; For ($i=0; $i -le ($_.AssignedLicenses | Measure).Count ; $i++) { If( [string]::IsNullOrEmpty( $_.AssignedLicenses[$i].SkuId ) -ne $True) { $licensed=$true } } ; If( $licensed -eq $false) { Write-Host $_.UserPrincipalName} }
To view the list of all user accounts in your organization that have been assigned any of your licensing plans (licensed users), run the following command:
Get-AzureAdUser | ForEach { $licensed=$False ; For ($i=0; $i -le ($_.AssignedLicenses | Measure).Count ; $i++) { If( [string]::IsNullOrEmpty( $_.AssignedLicenses[$i].SkuId ) -ne $True) { $licensed=$true } } ; If( $licensed -eq $true) { Write-Host $_.UserPrincipalName} }
Note
To list all of the users in your subscription, use the Get-AzureAdUser -All $true
command.
Use the Microsoft Azure Active Directory Module for Windows PowerShell
First, connect to your Microsoft 365 tenant.
To view the list of all user accounts and their licensing status in your organization, run the following command in PowerShell:
Get-MsolUser -All
Note
PowerShell Core does not support the Microsoft Azure Active Directory Module for Windows PowerShell module and cmdlets with Msol in their name. To continue using these cmdlets, you must run them from Windows PowerShell.
To view the list of all unlicensed user accounts in your organization, run the following command:
Get-MsolUser -All -UnlicensedUsersOnly
To view the list of all licensed user accounts in your organization, run the following command:
Get-MsolUser -All | where {$_.isLicensed -eq $true}
See also
Manage Microsoft 365 user accounts, licenses, and groups with PowerShell
Feedback
Submit and view feedback for