当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在 Microsoft Graph PowerShell 中使用 Get-MgUser

作者:精品下载站 日期:2024-12-14 05:52:40 浏览:14 分类:玩电脑

如何在 Microsoft Graph PowerShell 中使用 Get-MgUser


您需要使用 Get-MgUser PowerShell cmdlet 获取有关 Microsoft Entra ID 中所有用户的信息。 Get-MgUser 检索每个用户对象的数据。在本文中,您将了解如何在 Microsoft Graph PowerShell 中使用 Get-MgUser cmdlet。

安装 Microsoft Graph PowerShell

在开始之前,必须安装 Microsoft Graph PowerShell 模块,包括 Microsoft Graph Beta 模块。

运行以下命令来安装 Microsoft Graph 模块。

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

重要提示:始终安装 Microsoft Graph PowerShellMicrosoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。

连接到 Microsoft Graph PowerShell

您需要使用以下范围连接到 Microsoft Graph PowerShell。

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

在接下来的步骤中,您将设置为将 Get-MgUser cmdlet 与 Microsoft Graph PowerShell 结合使用。

获取所有 Microsoft 365 用户的列表

Get-MgUser cmdlet 检索 Microsoft Entra ID 中的所有用户,这些用户是组织中的许可和未许可用户、共享邮箱、会议室和设备邮箱。

要获取 Microsoft Entra ID 中所有用户的列表,您需要使用带有 -All 参数的 Get-MgUser PowerShell cmdlet。否则,您将只能获得按字母顺序排列的前 100 位用户。

运行以下 PowerShell 命令以检索 Microsoft Entra ID 中的所有用户。

Get-MgUser -All

PowerShell 输出显示 Microsoft Entra ID 中所有已许可和未许可用户的列表及其 DisplayNameIdMail用户主体名称

邮件标题下没有输出结果的用户是没有邮箱的用户。

DisplayName     Id                                   Mail                         UserPrincipalName
-----------     --                                   ----                         -----------------
Adam Mackay     0de964ae-f09a-4e65-bc8c-cbeac2745e4c [email protected]     [email protected]
Andrea Baker    7bfec79d-7806-484a-ac83-133cd4cf5af5 [email protected]    [email protected]
Bella Carr      9a88d528-537d-43da-a7d3-40ae600dea16                              [email protected]
Benjamin May    2beda4b9-559b-4d39-9415-51ce47f2963f                              [email protected]
Bernadette Bond 6ec79c8e-5a84-4992-b2df-4caf3f997a74 [email protected] [email protected]
Blake Martin    5f4d37cd-383c-413f-87a2-aab0dc6a531a [email protected]    [email protected]
Boris Lean      dc72baf0-e44f-48c3-a459-8005a9240f6f                              [email protected]
Brandon Grath   9706949a-e756-41c1-a90a-113f4f777e4b                              [email protected]
Brenda Smith    0f38d53f-cbe0-4844-86e9-1032a45ba31b [email protected]    [email protected]

获取成员属性列表

若要获取特定对象(例如 Microsoft 365 用户帐户)的所有可用属性的列表,您需要添加 Get-Member PowerShell cmdlet。

运行以下 PowerShell 命令以获取成员的所有属性的列表。

Get-Mguser | Get-Member

获取所有用户帐户的计数

要获取组织中所有用户的计数,您需要添加 Measure-Object cmdlet

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

获取所有获得许可的 Microsoft 365 用户

您可以使用 Get-MgUser PowerShell cmdlet 查找组织中的许可用户。我们想要显示组织中已分配任何许可计划的所有用户帐户的列表。

要过滤用户,您可以使用比较运算符(eq、ne、le、gt、startsWith、endsWith)和逻辑运算符(and、or)

获取所有已分配许可证的许可用户

使用-Filter参数获取所有没有访客的许可用户。然后,您将使用ne(不等于)运算符和-Consistencylevel eventual

运行以下 PowerShell 命令以查看所有许可用户的列表。

Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable licensedUserCount -Select UserPrincipalName, DisplayName, AssignedLicenses | Format-Table -Property UserPrincipalName, DisplayName, AssignedLicenses

PowerShell 结果输出显示所有许可用户及其分配的许可证

UserPrincipalName           DisplayName    AssignedLicenses
-----------------           -----------    ----------------
[email protected]   Brenda Smith   {c42b9cae-ea4f-4ab7-9717-81576235ccac}
[email protected]     Ken Walker     {c42b9cae-ea4f-4ab7-9717-81576235ccac}
[email protected]     Søren Vest     {c42b9cae-ea4f-4ab7-9717-81576235ccac}
[email protected]    Diana Baker    {c42b9cae-ea4f-4ab7-9717-81576235ccac}
[email protected] Stephen Hunter {c42b9cae-ea4f-4ab7-9717-81576235ccac}
[email protected]    Ryan Walker    {c42b9cae-ea4f-4ab7-9717-81576235ccac}
[email protected]   Susan Brown    {c42b9cae-ea4f-4ab7-9717-81576235ccac}

获取除访客之外的所有许可用户

获取所有许可用户而不显示分配的许可证。 PowerShell 输出将显示 DisplayNameIdMailUserPrincipalName

运行以下 PowerShell 命令以获取所有许可用户的列表。

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

获取所有许可用户,包括访客

获取所有许可的用户和来宾,而不显示分配的许可证。 PowerShell 输出将显示 DisplayNameIdMailUserPrincipalName

运行以下 PowerShell 命令以获取所有许可用户(包括来宾)的列表。

Get-MgUser -All -Filter 'assignedLicenses/$count ne 0' -ConsistencyLevel eventual -CountVariable licensedUserCount

获取所有未经许可的 Microsoft 365 用户

您可以使用 Get-MgUser PowerShell cmdlet 仅查找组织中未经许可的用户。我们想要显示组织中尚未分配任何许可计划的所有用户帐户的列表。

获取除访客之外的所有未授权用户

使用-Filter参数获取所有没有访客的未经许可的用户。您还必须将 eq(等于)比较运算符与 -ConsistencyLevel eventual 结合使用。

运行以下 PowerShell 命令以查看所有未经许可的用户的列表。

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

PowerShell 输出结果显示组织中未经许可的用户。

DisplayName          Id                                   Mail                 UserPrincipalName
-----------          --                                   ----                 -----------------
Christian Morgan 05be018c-cb06-480e-a530-1d7ffef00451                          [email protected]
Tim Lewis        0615caeb-b1b2-483f-8c8a-c5384168c7bd                          [email protected]
Michael Smith    0680abc1-001f-4007-9a17-57e5cff2a303                          [email protected]
Keith Rutherford 07c4f0b5-7c2b-409b-8d09-eb66954c2f83                          [email protected]
Emma Cornish     07d51ef8-4663-4f5a-bd2f-a84784984a15                          [email protected]
Adam Mackay      0de964ae-f09a-4e65-bc8c-cbeac2745e4c [email protected] [email protected]
Catch All        182292ee-eaec-438b-bf14-f25dec9cf1cd [email protected]   [email protected]

获取所有未经许可的用户,包括访客

运行以下 PowerShell 命令以获取所有未经许可的用户,包括来宾。

Get-MgUser -All -Filter 'assignedLicenses/$count eq 0' -ConsistencyLevel eventual -CountVariable unlicensedUserCount

获取禁用的用户帐户

当用户被阻止登录 Microsoft 365 服务时,该用户帐户已被禁用。我们想要获取这些(被阻止的)禁用用户帐户的列表。详细了解如何阻止 Microsoft 365 用户登录。

有两种方法可以获取禁用的用户帐户。

选项 1

我们希望将-Filtereq(等于)运算符一起使用。

Get-MgUser -All -Filter "accountEnabled eq false"

选项 2

我们希望将-Filterne(不等于)运算符一起使用。因此,您必须添加 -CountVariable CountVar-Consistencylevel eventual

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

获取启用的用户帐户

当用户帐户可以登录 Microsoft 365 服务时,它就是一个已启用的用户帐户。我们想要获取这些(未阻止的)启用的用户帐户的列表。详细了解如何取消阻止 Microsoft 365 用户登录。

有两种方法可以获取启用的用户帐户。

选项 1

-Filter 参数与eq(等于)运算符结合使用来获取活动用户帐户。

Get-MgUser -All -Filter "accountEnabled eq true"

选项 2

我们希望将 -Filter 参数与 ne(不等于)运算符一起使用。因此,您必须添加 -CountVariable CountVar-Consistencylevel eventual

运行以下 PowerShell 命令以获取活动用户帐户。

Get-MgUser -All -Filter "accountEnabled ne false" -CountVariable CountVar -ConsistencyLevel eventual

获取所有用户登录活动日期和时间

还有 Get-MgBetaUser PowerShell cmdlet,您需要使用它来获取正确的结果。

注意:Microsoft 在将新属性最终添加到 Get-MgUser cmdlet 之前,将其添加到 Beta cmdlet。

要获取活跃和非活跃用户,我们需要查看用户上次登录的时间。上次登录活动将告诉您用户何时处于活跃状态。

运行以下 PowerShell 命令以获取单个用户的登录活动。

Get-MgBetaUser -Filter "UserPrincipalName eq '[email protected]'" -Property SignInActivity | Select-Object -ExpandProperty SignInActivity | fl

PowerShell 输出结果显示用户上次成功登录是在 2024 年 2 月 26 日。

LastNonInteractiveSignInDateTime  : 26/02/2024 17.58.31
LastNonInteractiveSignInRequestId : 9ce398f0-741d-4a97-ab90-ce204fa29a00
LastSignInDateTime                : 20/02/2024 09.46.43
LastSignInRequestId               : 0ccb2b7e-ceac-4d45-9552-9ba4d4580d01
LastSuccessfulSignInDateTime      : 26/02/2024 17.58.31
LastSuccessfulSignInRequestId     : 9ce398f0-741d-4a97-ab90-ce204fa29a00
AdditionalProperties              : {}

要获取所有用户的登录活动,您可以查看哪些用户处于活动状态和非活动状态。

运行以下 PowerShell 命令以获取所有用户上次登录 Out-GridView 的活动。

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

将出现 Out-GridView,您可以在其中搜索和过滤属性。要获取更详细的信息,您可以导出 Microsoft 365 用户上次登录日期和时间。

[玩转系统] 如何在 Microsoft Graph PowerShell 中使用 Get-MgUser

获取用户角色

我们将使用 Get-MgDirectoryRole cmdlet 检索目录角色对象的属性。您可以获得具有全局管理员角色的 Microsoft 365 用户帐户列表。

运行以下 PowerShell 脚本以获取用户角色。

$roles = Get-MgDirectoryRole

$output = foreach ($role in $roles) {
    $roleId = $role.Id
    $userList = Get-MgDirectoryRoleMember -DirectoryRoleId $roleId
    $users = foreach ($user in $userList) {
        $userDetails = Get-MgUser -UserId $user.id
        [PSCustomObject]@{
            Role     = $role.DisplayName
            UPN      = $userDetails.UserPrincipalName
            UserName = $userDetails.DisplayName
            Mail     = $userDetails.Mail
        }
    }
    $users
}
$output

PowerShell 输出显示不同的用户及其角色。

Role                   UPN                  UserName
----                   ---                  --------      
Exchange Administrator [email protected]   M365info Admin
Exchange Administrator [email protected] M365info Admin
Global Administrator   [email protected]   M365info Admin
Global Administrator   [email protected] M365info Admin

获取所有用户的 CreatedDateTime

使用 Get-MgBetaUser PowerShell cmdlet 检查所有用户的CreatedDateTime

Get-MgBetaUser -All | Select-Object DisplayName, UserPrincipalName, CreatedDateTime

PowerShell 结果显示所有用户及其创建日期时间的列表。

DisplayName   UserPrincipalName          CreatedDateTime
-----------   -----------------          ---------------
Amanda Hansen [email protected] 06/03/2023 11.06.56
Andrea Baker  [email protected]  02/10/2023 09.17.04
Anna Bell     [email protected]     06/11/2023 21.55.22
Austin Mathis [email protected] 02/10/2023 09.17.10
Bella Carr    [email protected]    02/10/2023 09.17.06
Brenda Smith  [email protected]  06/11/2023 21.59.33
George Wilson [email protected] 28/09/2023 20.01.47

获取单用户CreatedDateTime

使用以下 PowerShell 命令检查单个用户的创建日期时间

Get-MgBetaUser -UserId "[email protected]" | Select-Object DisplayName, UserPrincipalName, CreatedDateTime

PowerShell 输出显示用户(Amanda Hansen) 创建日期和时间。

DisplayName   UserPrincipalName          CreatedDateTime
-----------   -----------------          ---------------
Amanda Hansen [email protected] 06/03/2023 11.06.56

获取以电话号码开头的用户

为了获取手机号码以 +44 开头的所有用户,我们将使用 -like 运算符。

运行以下 PowerShell 命令。

Get-MgUser -All | Where-Object {$_.MobilePhone -like "+44*"} | Select-Object UserPrincipalName, DisplayName, MobilePhone

PowerShell 输出结果显示所有拥有以 +44 开头的手机号码的用户。

UserPrincipalName          DisplayName   MobilePhone
-----------------          -----------   -----------
[email protected] Amanda Hansen +44 20 1285 6677

要获取使用以 +44 开头的商务电话的所有用户,请运行以下 PowerShell 命令。

Get-MgUser -All | Where-Object {$_.BusinessPhones -like "+44*"} | Select-Object UserPrincipalName, DisplayName, BusinessPhones

PowerShell 输出显示拥有以 +44 开头的商务电话号码的所有用户。

UserPrincipalName          DisplayName   BusinessPhones
-----------------          -----------   --------------
[email protected]   Adam Mackay   {+44 20 8885 6677}
[email protected] Amanda Hansen {+44 20 1285 6673}

获取以特定电子邮件地址结尾的用户

要检索以特定电子邮件地址结尾的用户,我们将添加 endsWith 运算符。

运行以下 PowerShell 示例来过滤以特定电子邮件地址(a-d.site)结尾的所有用户。

Get-MgUser -All -Filter "endsWith(mail,'a-d.site')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar

PowerShell 输出将检索具有该特定电子邮件地址的用户列表(a-d.site)

DisplayName     Id                                   Mail                              UserPrincipalName
-----------     --                                   ----                              -----------------
Amanda Hansen   41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 [email protected]        [email protected]
Andrea Baker    7bfec79d-7806-484a-ac83-133cd4cf5af5 [email protected]         [email protected]
Anna Bell       82cd0d62-e974-4892-aca6-e0387abc62be [email protected]            [email protected]
Bernadette Bond 6ec79c8e-5a84-4992-b2df-4caf3f997a74 [email protected]      [email protected]
Blake Martin    5f4d37cd-383c-413f-87a2-aab0dc6a531a [email protected]         [email protected]
Brenda Smith    0f38d53f-cbe0-4844-86e9-1032a45ba31b [email protected]         [email protected]

获取以特定电子邮件地址开头的用户

要检索以特定邮件地址开头的用户,我们将添加 startsWith 运算符。

运行下面的 PowerShell 示例来过滤所有以字母 (an) 开头的电子邮件地址开头的用户。

Get-MgUser -All -Filter "startsWith(mail,'an')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar

PowerShell 输出将检索电子邮件地址an开头的用户列表。

DisplayName  Id                                   Mail                      UserPrincipalName
-----------  --                                   ----                      -----------------
Andrea Baker 7bfec79d-7806-484a-ac83-133cd4cf5af5 [email protected] [email protected]
Anna Bell    82cd0d62-e974-4892-aca6-e0387abc62be [email protected]    [email protected]
Anne Butler  5cae3874-442b-459c-8f33-3aee5b879275 [email protected]  [email protected]

按使用位置获取用户

要按使用位置过滤所有用户,您需要使用有效的国家/地区代码。如果您输入整个国家/地区名称,它将不起作用。

在我们的示例中,我们希望获取荷兰的用户使用位置,因此我们需要在 PowerShell 中使用NL

Get-MgUser -All -Filter "UsageLocation eq 'NL'"

输出显示使用位置位于荷兰的所有用户的列表。

DisplayName    Id                                   Mail                              UserPrincipalName
-----------    --                                   ----                              -----------------
RoomTest8      274d72d7-cc30-4a64-bc33-c99ff96c3abf [email protected]            [email protected]
Info Box       479a0514-b10f-491a-ab2a-ec92c9673a35 [email protected]              [email protected]
Projector 21   6f4d2832-2753-4433-aba9-11dd73c14e39 [email protected]          [email protected]
Julia Wood     1a1fd41c-ca5f-4432-8313-7a662576856b                                   [email protected]
Stephen Hunter c32b2b27-d809-439a-a3e3-eb7a749eeb72 [email protected]       [email protected]
Amanda Morgan  f8261d51-3df9-4f21-a6b1-533412669c11 [email protected]        [email protected]
Kevin Howard   20a534e1-0b53-42f0-b119-ad87e29e39da                                   [email protected]
Lauren Russell 188f1cf6-ed47-4255-a776-d55a1fb40187                                   [email protected]

要获取没有使用位置的 Microsoft 365 用户帐户的列表,我们需要使用 $null 值。

运行以下 PowerShell 命令来查找没有使用位置值的用户。

Get-MgUser -All | Where-Object { $_.UsageLocation -eq $null } | Select-Object UserPrincipalName, UsageLocation

获取来自特定国家/地区的用户

使用-Filter参数获取来自特定国家的所有用户。

运行以下 PowerShell 命令以获取来自美国的所有用户。

Get-MgUser -All -Filter "Country eq 'USA'"

PowerShell 输出显示来自美国的用户列表。

DisplayName   Id                                   Mail                       UserPrincipalName
-----------   --                                   ----                       -----------------
Amanda Morgan f8261d51-3df9-4f21-a6b1-533412669c11 [email protected] [email protected]   
Anna Bell     82cd0d62-e974-4892-aca6-e0387abc62be [email protected]     [email protected]       
Brenda Smith  0f38d53f-cbe0-4844-86e9-1032a45ba31b [email protected]  [email protected] 

要检索不带任何国家/地区的所有用户,我们需要将 Where$null 值一起使用。

运行以下 PowerShell 命令来过滤所有不带任何国家/地区的用户。

Get-MgUser -All | Where-Object { $_.Country -eq $null } | Select-Object UserPrincipalName, Country

获取特定州的用户

要按特定状态过滤用户,我们需要使用eq比较运算符。

运行以下 PowerShell 命令以获取特定州(纽约)的所有用户。

Get-MgUser -All -Filter "State eq 'New York'"

PowerShell 输出显示来自同一州(纽约)的用户列表。

DisplayName   Id                                   Mail                       UserPrincipalName
-----------   --                                   ----                       -----------------
Amanda Hansen 41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 [email protected] [email protected]   
Adam Mackay   0de964ae-f09a-4e65-bc8c-cbeac2745e4c [email protected]   [email protected]   

要检索没有任何状态的所有用户,我们需要将 Where$null 值一起使用。

运行以下 PowerShell 命令来过滤所有没有状态的用户。

Get-MgUser -All | Where-Object { $_.Country -eq $null } | Select-Object UserPrincipalName, State

获取特定城市的用户

要按特定城市过滤用户,我们需要使用eq比较运算符。

运行以下 PowerShell 命令以获取特定城市(纽约)的所有用户。

Get-MgUser -All -Filter "City eq 'New York'"

PowerShell 输出显示来自同一城市(纽约)的用户列表。

DisplayName   Id                                   Mail                       UserPrincipalName
-----------   --                                   ----                       -----------------
Amanda Hansen 41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 [email protected] [email protected]  

要检索没有任何城市的所有用户,我们需要将Where$null值一起使用。

运行以下 PowerShell 命令来过滤所有没有城市的用户。

Get-MgUser -All | Where-Object { $_.City -eq $null } | Select-Object UserPrincipalName, City

获取特定部门的用户

检索组织中特定部门的所有用户。

在我们的示例中,我们希望获取营销部门的所有用户。

Get-MgUser -All -Filter "Department eq 'Marketing'"

PowerShell 输出显示来自 Marketing 的用户 DisplayNameIdMailUserPrincipalName 部门。

DisplayName   Id                                   Mail                       UserPrincipalName
-----------   --                                   ----                       -----------------
Amanda Hansen 41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 [email protected] [email protected]
Anne Butler   5cae3874-442b-459c-8f33-3aee5b879275 [email protected]   [email protected]
Brian Mill    411a8f10-0dfa-4034-a1e3-a8b6e4cad2f6 [email protected]    [email protected]

按职位获取用户

获取组织中具有特定职位的所有用户。

在我们的示例中,我们想要检索来自不同部门的所有经理

Get-MgUser -All -Filter "JobTitle eq 'Manager'"

PowerShell 输出显示所有职位经理的用户。

DisplayName   Id                                   Mail                       UserPrincipalName
-----------   --                                   ----                       -----------------
Søren Vest    1e367b85-f0c0-4c9c-a16a-22d132f1d8e6 [email protected]    [email protected]
Amanda Morgan f8261d51-3df9-4f21-a6b1-533412669c11 [email protected] [email protected]
Andrea Baker  7bfec79d-7806-484a-ac83-133cd4cf5af5 [email protected]  [email protected]
Adam Mackay   0de964ae-f09a-4e65-bc8c-cbeac2745e4c [email protected]   [email protected]
Brian Mill    411a8f10-0dfa-4034-a1e3-a8b6e4cad2f6 [email protected]    [email protected]
Brenda Smith  0f38d53f-cbe0-4844-86e9-1032a45ba31b [email protected]  [email protected]

获取用户上次密码更改日期时间

您可以检查组织中的所有 Microsoft 365 用户上次更改密码的时间。

运行以下 PowerShell 命令以获取所有用户的上次密码更改日期时间。

Get-MgUser -All -Property DisplayName, LastPasswordChangeDateTime | Select-Object DisplayName, LastPasswordChangeDateTime

PowerShell 结果显示用户列表及其上次密码更改日期时间。

DisplayName      LastPasswordChangeDateTime
-----------      --------------------------
Amanda Hansen    26/10/2023 09.07.23
Anna Bell        21/11/2023 14.29.15
Ben Andrews      15/02/2024 10.21.17
Brenda Smith     21/02/2024 09.34.57
Carl Hawk        06/11/2023 21.59.04
Chris Lucas      26/10/2023 09.04.25
Christian Morgan 22/01/2024 09.21.38
Francis Kent     06/11/2023 22.01.09
Harry Longmuir   15/02/2024 10.21.17
Ken Walker       28/11/2023 08.40.57
Ken Walker       13/12/2023 10.02.15
Mark             03/11/2023 12.03.22
Thomas Lee       12/02/2024 10.52.20

您还可以获取上个月更改密码的所有 Microsoft 365 用户。您需要添加 -ge 相等比较运算符并指定日期。

运行以下 PowerShell 命令以获取用户上个月的密码更改。

Get-MgUser -All -Property DisplayName, LastPasswordChangeDateTime |
Where-Object { $_.LastPasswordChangeDateTime -ge (Get-Date).AddMonths(-1) } |
Select-Object DisplayName, LastPasswordChangeDateTime

PowerShell 输出显示上个月内更改密码的用户列表。

DisplayName    LastPasswordChangeDateTime
-----------    --------------------------
Ben Andrews    15/02/2024 10.21.17
Brenda Smith   21/02/2024 09.34.57
Harry Longmuir 15/02/2024 10.21.17
Thomas Lee     12/02/2024 10.52.20

如果您想获取所有 Microsoft 365 用户的详细密码信息,您需要导出 Microsoft 365 用户密码报告。

获取用户密码永不过期

您可以检查哪些用户设置了密码永不过期,因为 Microsoft 建议所有用户都这样做。管理 Microsoft 365 用户密码,将整个组织的密码设置为永不过期。

运行以下 PowerShell 命令来查看哪些用户已设置(true)或未设置(false)密码永不过期

Get-MgUser -All -Property UserPrincipalName, PasswordPolicies | Select-Object UserprincipalName, @{
    Name       = "PasswordNeverExpires";
    Expression = { $_.PasswordPolicies -contains "DisablePasswordExpiration" }
}

它显示所有 Microsoft 365 用户帐户的列表,包括来宾。

UserPrincipalName             PasswordNeverExpires
-----------------             --------------------
[email protected]    False
[email protected]     True
[email protected]        False
[email protected]      False
[email protected]     False
[email protected]      True
[email protected]    True
[email protected]       True
[email protected]  False
[email protected]        True
[email protected]        True
[email protected]     False
[email protected]      True
[email protected]       True

获取本地 Active Directory 同步用户

我们想要显示所有 Microsoft 365 用户帐户的列表:

  • 非同步用户是云端的所有用户
  • 同步的用户都是本地用户,但通过 Microsoft Entra Connect 工具同步到云端

要获取所有云用户的列表,请运行以下 PowerShell 命令。

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

要获取从本地 Active Directory 同步的 Microsoft 365 帐户列表(包括其 OnPremisesImmutableId),请运行以下 PowerShell 命令

Get-MgUser -All -Filter "OnPremisesSyncEnabled eq true" -Property OnPremisesImmutableId, DisplayName, Id, Mail, UserPrincipalName, OnPremisesLastSyncDateTime | Select-Object DisplayName, Id, Mail, UserPrincipalName, OnPremisesImmutableId, OnPremisesLastSyncDateTime | Out-GridView

就是这样!

了解更多:如何更改 Microsoft 365 用户默认 MFA 方法 »

结论

您学习了如何在 Microsoft Graph PowerShell 中使用 Get-MgUser cmdlet。若要查找特定的 Microsoft 365 用户属性,您必须使用正确的命令来筛选结果。

您喜欢这篇文章吗?您可能还喜欢使用 Microsoft Graph PowerShell 导出 Azure AD 用户。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯