使用 PowerShell 檢視授權和未授權的 Microsoft 365 使用者

本文適用於 Microsoft 365 企業版和 Office 365 企業版。

Microsoft 365 組織中的用戶帳戶可能會從貴組織中可用的授權方案指派一些、全部或無可用的授權。 您可以使用 PowerShell for Microsoft 365 快速尋找組織中授權和未授權的使用者。

注意事項

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

開始使用適用於 Microsoft 365 的 PowerShell