使用 PowerShell 檢視授權和未授權的Microsoft 365使用者
本文適用於 Microsoft 365 企業版和 Office 365 企業版。
您Microsoft 365組織中的使用者帳戶可能會從貴組織中可用的授權方案指派一些、全部或無可用的授權。 您可以使用 PowerShell for Microsoft 365,快速尋找組織中授權和未授權的使用者。
使用 Microsoft Graph PowerShell SDK
讀取使用者屬性,包括授權詳細資料需要 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."
針對 Graph 模組,請使用 Azure Active Directory PowerShell
若要檢視組織中未獲指派任何授權方案的所有使用者帳戶清單, (未授權的使用者) ,請執行下列命令:
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
命令。
使用適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組。
若要檢視組織中所有使用者帳戶及其授權狀態的清單,請在 PowerShell 中執行下列命令:
Get-MsolUser -All
注意
PowerShell Core 不支援適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組和名稱有 Msol 的 Cmdlet。 若要繼續使用這些 Cmdlet,您必須從 Windows PowerShell 執行。
若要檢視您組織中所有未經授權使用者帳戶的清單,執行下列命令:
Get-MsolUser -All -UnlicensedUsersOnly
若要檢視您組織中所有經授權使用者帳戶的清單,執行下列命令:
Get-MsolUser -All | where {$_.isLicensed -eq $true}
另請參閱
以 PowerShell 管理 Microsoft 365 使用者帳戶、授權和群組