如何在PowerShell中使用Get-Mguser

Microsoft Graph PowerShell中的Get-Mguser CMDLET在Microsoft Entra ID中检索所有用户详细信息。您可以从组织中获取所有Microsoft 365用户或特定用户。虽然您可以在Microsoft Entra Admin Center或Microsoft 365 Admin Center中获取所有用户,但您始终可以做更多的事情,并且可以使用PowerShell。在本文中,您将学习如何在PowerShell中使用Get-Mguser cmdlet。

开始之前

在进行进一步之前,您必须安装并连接到Microsoft Graph PowerShell,这一点很重要。否则,Get-Mguser cmdlet将无法使用。

安装Microsoft Graph PowerShell

以管理员的身份运行PowerShell并安装Microsoft Graph PowerShell模块。

Install-Module Microsoft.Graph -Force

重要的:在运行CMDLET或脚本以防止错误和错误结果之前,请务必更新到最新的Microsoft Graph PowerShell模块版本。

连接到Microsoft Graph PowerShell

您需要使用正确的权限连接到Microsoft Graph PowerShell。如果不这样做,则无法使用Get-Mguser CMDLET检索用户结果。

Connect-MgGraph -Scopes "User.Read.All", "AuditLog.Read.All"

您是否要在没有用户交互的情况下连接,因为您希望脚本自动运行?用基于证书的身份验证或客户秘密设置它。在文章中阅读更多信息,请连接到Microsoft Graph PowerShell。

获取用户信息

让我们首先从基础知识开始,这就是获取用户信息。

获取单个用户信息

要获取用户信息,请使用 - 用户参数并附加用户ID。

Get-MgUser -UserId "944d57a0-0d24-4d55-ac5b-e9b741be9031"

您也可以使用用户principalname获取用户信息。

Get-MgUser -UserId "[email protected]"

添加格式列表cmdlet获取属性列表。

建议阅读:如何使用PowerShell和GPO添加或删除固定文件夹以快速访问

Get-MgUser -UserId "[email protected]" | Format-List

获取所有用户信息

运行Get-mguser cmdlet,包括-全部参数,检索所有用户。

笔记:始终使用-全部参数以获取所有结果。否则,只会出现100个项目。

Get-MgUser -All

为了计算所有用户,我们将添加测量对象命令的cmdlet。

Get-MgUser -All | Measure-Object | Select-Object -ExpandProperty Count

获取用户帐户状态

要获得用户帐户状态,我们必须添加-财产参数,包括AccountEnabled属性。否则,AccountEnabled值显示为空。

获取单个用户帐户状态

获取单个用户的帐户状态。

Get-MgUser -UserId "[email protected]" -Property Id, DisplayName, UserPrincipalName, AccountEnabled | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled

获取所有用户帐户状态

获取所有用户帐户状态。

Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, AccountEnabled | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled

您随时可以添加-筛选参数,仅检索启用帐户。

Get-MgUser -All -Filter "accountEnabled eq true" -Property Id, DisplayName, UserPrincipalName, AccountEnabled | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled

或仅过滤禁用的用户帐户。

Get-MgUser -All -Filter "accountEnabled ne true" -ConsistencyLevel eventual -CountVariable CountVar -Property Id, DisplayName, UserPrincipalName, AccountEnabled | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled

要知道您可以添加哪个属性以从Get-Mguser CMDLET获取用户属性信息,请使用Microsoft文档表

获取所有云用户

如果您有混合环境,则将用户从本地广告同步到Microsoft Entra ID。本地广告是您的域名授权,您应该在那里创建用户。但是,组织中可能有一些用户直接在云中创建。

获取包括客人在内的所有云用户

让我们仅在云用户上过滤。这包括来宾帐户。

笔记:使用-筛选带有的参数这是操作员。默认情况下不支持此请求,因为这是仅在高级查询中支持操​​作员。因此,您必须添加一致性标题设置为最终并使用Countvar查询字符串。

Get-MgUser -All -Filter "OnPremisesSyncEnabled ne true" -ConsistencyLevel eventual -CountVariable CountVar

让所有云用户不包括客人

现在,让我们仅过滤没有客人帐户的云用户。

Get-MgUser -All -Filter "OnPremisesSyncEnabled ne true and UserType eq 'Member'" -ConsistencyLevel eventual -CountVariable CountVar

获取所有云的访客用户

仅对云中的客人用户过滤。

Get-MgUser -All -Filter "OnPremisesSyncEnabled ne true and UserType eq 'Guest'" -ConsistencyLevel eventual -CountVariable CountVar

获取许可用户

并非所有用户都获得许可。租户中有或没有许可证的用户。要确切知道哪些用户帐户已分配了许可证,哪些未分配了许可证,我们可以对此进行特定过滤。

获取所有许可用户

仅在许可用户上过滤。

Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records

获取所有无执照的用户

仅在无执照的用户上过滤。

Get-MgUser -All -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records

获取所有许可和阻止用户

您可以阻止用户,他们仍然有许可证。让我们通过许可证获得所有被阻止的用户。

Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and accountEnabled eq false" -ConsistencyLevel eventual -CountVariable Records

让本地同步用户

仅获取与云同步并在显示名称上对其进行排序的本地用户。

Get-MgUser -All -Filter "OnPremisesSyncEnabled eq true" | Sort-Object DisplayName

获取用户的经理

并非所有用户都有经理,但是其中一些用户会这样做。我们喜欢了解用户或所有用户的经理。

获取单个用户的经理

让我们检查一下单个用户的经理是谁。

如果您运行下面的CMDLET来检索单个用户的经理,则将看不到经理。

Get-MgUser -UserId "[email protected]" | Select-Object DisplayName, Manager

您会看到它显示了Microsoft.graph.powershell.models.microsoftgraphdirectoryobject的值。

DisplayName   Manager
-----------   -------
Amanda Morgan Microsoft.Graph.PowerShell.Models.MicrosoftGraphDirectoryObject

那是因为您需要添加- expandproperty范围。

Get-MgUser -UserId "[email protected]" -ExpandProperty Manager | Select-Object @{Name = 'Manager'; Expression = { $_.Manager.AdditionalProperties.mail } }

另一种方法是将管理器属性存储在变量中,然后显示。

$user = Get-MgUser -UserId "[email protected]" -ExpandProperty Manager
$user.Manager.AdditionalProperties.mail

获取所有用户的经理

要知道将哪个经理分配给哪个用户,我们可以获取所有用户及其经理的列表。

Get-MgUser -All -ExpandProperty Manager | Select-Object UserPrincipalName, @{Name = 'Manager'; Expression = { $_.Manager.AdditionalProperties.mail } }

获取以显示名称开头的用户

我们可以添加Startswith操作员并检索以显示名称开头的所有结果。

Get-MgUser -All -Filter "startsWith(DisplayName,'Amanda')"

您还可以添加一个字母而不是单词。

Get-MgUser -All -Filter "startsWith(DisplayName,'A')"

获取以邮件地址结束的用户

添加末日操作员并检索所有以特定邮件地址结尾的用户,然后在显示名称上对其进行排序。

Get-MgUser -All -Filter "endsWith(mail,'exoip.com')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar

搜索以特定邮件地址结束的两个域。

Get-MgUser -All -Filter "endsWith(mail,'exoip.com') or endsWith(mail,'tajran.com')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar

获取用用户主名称结束的用户

添加末日操作员并检索所有以特定用户principalname结尾的用户,然后对显示名称进行排序。

Get-MgUser -All -Filter "endsWith(userprincipalname,'exoip.com')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar

让用户登录活动

最好在最后一次成功签名的用户过滤是一件好事。因此,我们可以理解该帐户是否正在使用。

获取单用户登录活动

检查单个用户的登录活动。

Get-MgUser -UserId "944d57a0-0d24-4d55-ac5b-e9b741be9031" -Property SignInActivity | Select-Object -ExpandProperty SignInActivity

如果要使用userprincipalname进行- 用户参数,您将获得错误。那是因为并非所有属性都接受。

Get-MgUser_Get: Get By Key only supports UserId and the key has to be a valid Guid

Status: 400 (BadRequest)
ErrorCode: 400

您可以做的是使用第二个Get-Mguser CMDLET检索用户ID,然后将其添加到第一个Get-Mguser CMDLET中。

Get-MgUser -UserId (Get-MgUser -UserId "[email protected]").Id -Property SignInActivity | Select-Object -ExpandProperty SignInActivity | fl

结果出现。

LastNonInteractiveSignInDateTime  : 20/01/2024 05:23:30
LastNonInteractiveSignInRequestId : 27e83e67-4c1c-4f74-babb-a55791680900
LastSignInDateTime                : 19/01/2024 11:31:20
LastSignInRequestId               : 6f659c53-cf13-4b7d-9fe5-93c6f9a52100
LastSuccessfulSignInDateTime      : 20/01/2024 05:23:30
LastSuccessfulSignInRequestId     : 27e83e67-4c1c-4f74-babb-a55791680900
AdditionalProperties              : {}

获取所有用户登录活动

让所有用户最后一次成功登录日期和时间,然后将输出发送到与单独窗口中的交互式表离格视图cmdlet。

Get-MgUser -All -Property Id, UserPrincipalName, DisplayName, SignInActivity | Select-Object Id, UserPrincipalName, DisplayName, @{Name = 'LastSuccessfulSignInDateTime'; Expression = { $_.SignInActivity.LastSuccessfulSignInDateTime } } | Out-GridView -Title "Last successful sign-in date"

获取具有公司名称的用户

您可以在组织中具有不同的公司名称。很高兴知道哪个用户属于哪个公司或在特定公司上过滤以缩小结果。

获取单个用户的公司名称

获取单个用户的公司名称。

Get-MgUser -UserId "[email protected]" -Property DisplayName, UserPrincipalName, CompanyName | Select-Object DisplayName, UserPrincipalName, CompanyName

获取所有用户的公司名称

获取所有用户帐户,包括公司名称。

Get-MgUser -All -Property DisplayName, UserPrincipalName, CompanyName | Select-Object DisplayName, UserPrincipalName, CompanyName

添加-筛选参数和Startswith操作员检索以特定公司名称开头的所有结果。

Get-MgUser -All -Property DisplayName, UserPrincipalName, CompanyName -ConsistencyLevel eventual -Count userCount -Filter "startsWith(CompanyName, 'Exo')" | Select-Object DisplayName, UserPrincipalName, CompanyName

您可以使用-搜索参数可以在公司上搜索并获得结果。

Get-MgUser -All -Search "CompanyName:Exoip" -ConsistencyLevel eventual

获取具有公司名称和帐户的用户

添加-搜索查找公司名称的参数和-筛选仅检索启用帐户的参数。

Get-MgUser -All -Filter "accountEnabled eq true" -Search "CompanyName:Exoip" -ConsistencyLevel eventual

了解如何在PowerShell脚本中添加Get-Mguser cmdlet的一种绝佳方法是浏览PowerShell脚本示例:

  • 使用PowerShell创建Microsoft Entra ID用户
  • 用PowerShell导出Microsoft Entra ID用户到CSV
  • 来自共享邮箱的块登录
  • 与PowerShell的Microsoft 365强制签名用户
  • 如何从Microsoft Entra中的应用中删除权限
  • 导出Microsoft 365残疾用户报告
  • 导出Microsoft 365非活动用户报告

结论

您学会了如何在PowerShell中使用Get-Mguser。 Get-Mguser CMDLET是从Microsoft Entra ID和Microsoft 365中检索用户的绝佳CMDLET。使用特定参数或组合它们根据希望结果出现的方式过滤搜索结果。

您喜欢这篇文章吗?您可能还喜欢如何导出条件访问策略。不要忘记关注我们并分享这篇文章。