使用 PowerShell 查看许可和未授权的 Microsoft 365 用户

此文章适用于 Microsoft 365 企业版和 Office 365 企业版。

Microsoft 365 组织中的用户帐户可能具有从组织中可用的许可计划分配给他们的部分、全部或全部可用许可证。 可以使用适用于 Microsoft 365 的 PowerShell 快速查找组织中的许可用户和未授权用户。

注意

Azure Active Directory 模块将替换为 Microsoft Graph PowerShell SDK。 可以使用 Microsoft Graph PowerShell SDK 访问所有 Microsoft Graph API。 有关详细信息,请参阅 Microsoft Graph PowerShell SDK 入门

使用 Microsoft Graph PowerShell SDK

首先, 使用 PowerShell 连接到 Microsoft 365

读取用户属性(包括许可证详细信息)需要 User.Read.All 权限范围或“获取用户”图形 API引用页中列出的其他权限之一。

读取租户中可用的许可证需要 Organization.Read.All 权限范围。

Connect-Graph -Scopes User.Read.All, Organization.Read.All

若要查看特定用户帐户的许可证详细信息,请运行以下命令:

Get-MgUserLicenseDetail -UserId "<user sign-in name (UPN)>"

例如:

Get-MgUserLicenseDetail -UserId "belindan@litwareinc.com"

若要查看组织中尚未分配任何许可计划 (未授权用户) 的所有用户帐户的列表,请运行以下命令:

Get-MgUser -Filter 'assignedLicenses/$count eq 0' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All

Write-Host "Found $unlicensedUserCount unlicensed users."

若要查看所有成员用户帐户的列表, (不包括组织中尚未分配任何许可计划 (未授权用户) 的来宾) ,请运行以下命令:

Get-MgUser -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All

Write-Host "Found $unlicensedUserCount unlicensed users (excluding guests)."

若要查看组织中已分配了任何许可计划的所有用户帐户的列表 (许可用户) ,请运行以下命令:

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

若要查看组织中分配有 E5 许可证的所有用户帐户的列表,请运行以下命令:

$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."

另请参阅

使用 PowerShell 管理 Microsoft 365 用户帐户、许可证和组

使用 PowerShell 管理 Microsoft 365

PowerShell for Microsoft 365 入门