使用 PowerShell 查看许可和未授权的 Microsoft 365 用户
此文章适用于 Microsoft 365 企业版和 Office 365 企业版。
Microsoft 365 组织中的用户帐户可能具有从组织中可用的许可计划分配给他们的部分、全部或全部可用许可证。 可以使用适用于 Microsoft 365 的 PowerShell 快速查找组织中的许可用户和未授权用户。
使用 Microsoft Graph PowerShell SDK
首先, 连接到 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."
使用用于图表模块的 Azure Active Directory PowerShell
首先, 连接到 Microsoft 365 租户。
若要查看组织中尚未分配任何许可计划 (未授权用户) 的所有用户帐户的列表,请运行以下命令:
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} }
若要查看组织中已分配了任何许可计划的所有用户帐户的列表 (许可用户) ,请运行以下命令:
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} }
注意
若要列出订阅中的所有用户,请使用 Get-AzureAdUser -All $true
命令。
使用Microsoft Azure Active Directory模块进行Windows PowerShell
首先, 连接到 Microsoft 365 租户。
若要查看组织中所有用户帐户及其许可状态的列表,请在 PowerShell 中运行以下命令:
Get-MsolUser -All
注意
PowerShell Core 不支持Windows PowerShell模块和名称为 Msol 的 cmdlet 的 Microsoft Azure Active Directory 模块。 若要继续使用这些 cmdlet,必须从 Windows PowerShell 运行它们。
若要查看组织中所有未授权用户帐户的列表,请运行以下命令:
Get-MsolUser -All -UnlicensedUsersOnly
若要查看组织中所有授权用户帐户的列表,请运行以下命令:
Get-MsolUser -All | where {$_.isLicensed -eq $true}
另请参阅
使用 PowerShell 管理 Microsoft 365 用户帐户、许可证和组