使用 PowerShell 檢視 Microsoft 365 用戶帳戶

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

您可以使用 Microsoft 365 系統管理中心 來檢視 Microsoft 365 租使用者的帳戶。 PowerShell for Microsoft 365 可啟用此功能,但也提供其他功能。

使用 Microsoft Graph PowerShell 檢視用戶帳戶

注意事項

Azure Active Directory (AzureAD) PowerShell 模組即將淘汰,並由 Microsoft Graph PowerShell SDK 取代。 您可以使用 Microsoft Graph PowerShell SDK 來存取所有的 Microsoft Graph API。 如需詳細資訊,請參閱 開始使用 Microsoft Graph PowerShell SDK

另請參閱 安裝 Microsoft Graph PowerShell SDK從 Azure AD PowerShell 升級至 Microsoft Graph PowerShell ,以取得如何分別安裝和升級至 Microsoft Graph PowerShell 的相關信息。

  1. 首先,安裝必要的軟體以使用 Microsoft Graph PowerShell。 如需詳細資訊,請參閱 使用 Microsoft Graph PowerShell 連線到 Microsoft 365

  2. 然後執行下列 Cmdlet,以使用必要的許可權範圍連線到您的組織,在此案例中為 User.ReadBasic.All

# Connect to Microsoft Graph
Connect-Graph -Scopes User.ReadBasic.All

檢視所有帳戶

若要顯示具有使用者識別碼和使用者主體名稱的使用者帳戶完整清單,請執行下列命令:

Get-MgUser -All | Select DisplayName,Id,UserPrincipalName

您應該取得類似以下的資訊:

DisplayName               Id                                   UserPrincipalName
-----------               --                                   -----------------
Conf Room Adams           6e206948-b2b6-406c-a728-80bbe78e4003 Adams@M365x89521157.OnMicrosoft.com
Adele Vance               916a6a08-b9d0-44b6-870f-562d8358a314 AdeleV@M365x89521157.OnMicrosoft.com
MOD Administrator         5710f237-df3f-4bcd-b875-82deb02f98aa admin@M365x89521157.onmicrosoft.com
Alex Wilber               8aa561dc-441d-4d74-aeb3-e2be41c116c8 AlexW@M365x89521157.OnMicrosoft.com
Allan Deyoung             6b629e5e-3cf4-42d0-8007-3a93f0253382 AllanD@M365x89521157.OnMicrosoft.com
Automate Bot              3a70feb4-9407-47b5-9b61-7526ac0e98d8 AutomateB@M365x89521157.OnMicrosoft.com      
Conf Room Baker           d8cf3fef-1d03-4b9c-9be0-fed44fb87596 Baker@M365x89521157.OnMicrosoft.com
Bianca Pisani             7fe8c2d1-eb8e-4032-96ba-26242ff0acd9 BiancaP@M365x89521157.OnMicrosoft.com        

檢視特定帳戶

若要顯示特定的用戶帳戶,請執行下列命令。 填入用戶帳戶的登入帳戶名稱,也稱為UPN) (用戶主體名稱。 拿掉“<” 和 “>” 字元。

Get-MgUser -UserId '<user principal name>'

以下為範例:

Get-MgUser -UserId 'BelindaN@litwareinc.onmicosoft.com'

檢視特定帳戶的其他屬性值

根據預設, Get-MgUser Cmdlet 只會顯示帳戶的 DisplayNameIdMailUserPrincipalName 屬性。

若要更選擇要顯示的屬性,請使用 Select Cmdlet 搭配 Get-MgUser Cmdlet。 若要結合兩個 Cmdlet,請使用“pipe” 字元 (“|”) ,這會指示PowerShell取得一個命令的結果,並將它傳送至下一個命令。 以下範例命令會顯示每個用戶帳戶的 DisplayNameDepartmentUsageLocation

Get-MgUser -All | Select DisplayName,Department,UsageLocation

此指令會指示 PowerShell:

  1. 取得 Get-MgUser) (使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。

  2. 只顯示使用者帳戶名稱、部門和使用位置 (選取 DisplayName、Department、UsageLocation) 。

若要查看特定用戶帳戶的所有屬性,請使用 Select Cmdlet 和通配符 (*) 。 以下為範例:

Get-MgUser -UserID 'BelindaN@litwareinc.onmicosoft.com' | Select *

另一個範例是,執行下列命令來檢查特定使用者帳戶的啟用狀態:

Get-MgUser -UserID '<sign-in name of the user account>' | Select DisplayName,UserPrincipalName,AccountEnabled

檢視帳戶同步處理狀態

使用者帳戶有兩個來源:

  • Windows Server Active Directory (AD) ,也就是從內部部署 AD 同步至雲端的帳戶。

  • Microsoft Entra 直接在雲端中建立的帳戶。

您可以使用下列命令來尋找從內部部署 AD 同步處理 帳戶。 它會指示PowerShell將 OnPremisesSyncEnabled 屬性設定為 True的所有使用者。

Get-MgUser -All -Filter 'OnPremisesSyncEnabled eq true'

您可以使用下列命令來尋找 僅限雲端的 帳戶。 它會指示PowerShell將 OnPremisesSyncEnabled 屬性設定為 False 或未設定 (Null) 的所有使用者。 從未從內部部署 AD 同步處理的帳戶,已將 OnPremisesSyncEnabled 設定為 Null。 最初從內部部署 AD 同步但不再同步的帳戶,已將 OnPremisesSyncEnabled 設定為 False

Get-MgUser -All | Where OnPremisesSyncEnabled -ne true
OnPremisesSyncEnabled```

### View accounts based on a common property

To be more selective about the list of accounts to display, you can use the **Where** cmdlet in combination with the **Get-MgUser** cmdlet. To combine the two cmdlets, use the "pipe" character ("|"), which tells PowerShell to take the results of one command and send it to the next command. Here is an example command that displays only those user accounts that have an unspecified usage location:
  
```powershell
Get-MgUser | Where UsageLocation -eq $Null

此指令會指示 PowerShell:

  1. 取得 Get-MgUser) (使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。

  2. (Where UsageLocation -eq $Null) ,尋找具有未指定使用位置的所有用戶帳戶。 命令會指示 PowerShell 只尋找未指定 UsageLocation 使用者帳戶屬性 (UsageLocation) 的帳戶集 (-eq $Null) 。

UsageLocation 屬性只是與用戶帳戶相關聯的許多屬性之一。 若要顯示特定用戶帳戶的所有屬性,請使用 Select Cmdlet 和通配符 (*) 。 以下為範例:

Get-MgUser -UserID BelindaN@litwareinc.onmicosoft.com | Select *

例如, City 是使用者帳戶屬性的名稱。 您可以使用下列命令來列出所有位於倫敦之使用者的帳戶:

Get-MgUser | Where City -eq "London"

提示

這些範例中 Where Cmdlet 的語法是 Where [user account property name] [comparison operator] [value] 。> [comparison operator] 是 -eq for equals, -ne for not equals, -lt for less than, -gt for greater than, and others. [value] 通常是字串, (一連串的字母、數位及其他字元) 、數值,或 $Null 為未指定。 如需詳細資訊,請參閱 Where

另請參閱

以 PowerShell 管理 Microsoft 365 使用者帳戶、授權和群組

使用 PowerShell 管理 Microsoft 365

開始使用適用於 Microsoft 365 的 PowerShell