使用 PowerShell 查看 Microsoft 365 用户帐户

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

可以使用 Microsoft 365 管理中心查看 Microsoft 365 租户的帐户。 Microsoft 365 的 PowerShell 支持此功能,但也提供了其他功能。

使用 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

查看所有帐户

若要显示具有用户 ID 和用户主体名称的用户帐户的完整列表,请运行以下命令:

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,请使用“管道”字符 (“|”) ,它告知 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 仅查找未 (-eq $Null) 指定 UsageLocation 用户帐户属性 (UsageLocation) 的帐户集。

UsageLocation 属性只是与用户帐户关联的许多属性之一。 若要显示特定用户帐户的所有属性,请使用 Select cmdlet 和通配符 (*) 。 下面是一个示例:

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

例如, City 是用户帐户属性的名称。 可以使用以下命令列出居住在伦敦的用户的所有帐户:

Get-MgUser | Where City -eq "London"

提示

这些示例中 Where cmdlet 的语法为 Where [用户帐户属性名称] [比较运算符] [value] 。> [比较运算符] 为 -eq 表示等于, -ne 表示不等于, -lt 表示小于, -gt 表示大于,等等。 [value] 通常是一个字符串, (字母、数字和其他字符的序列) 、数值或 未指定的$Null 。 有关详细信息,请参阅 Where

另请参阅

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

使用 PowerShell 管理 Microsoft 365

PowerShell for Microsoft 365 入门