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

[玩转系统] 管理 Microsoft 365 用户密码

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

管理 Microsoft 365 用户密码


Microsoft 建议将 Microsoft 365 密码过期策略设置为永不过期,因为密码过期要求弊大于利。当用户收到更改密码的消息或通知时,他们会在现有密码后面添加数字或符号并将其设置为新密码。这是有问题的。在本文中,您将了解如何使用 Microsoft Graph PowerShell 管理 Microsoft 365 用户密码。

密码过期策略

Microsoft 365 帐户具有无法更改的预定义密码策略。管理员可以让用户密码在一定天数后过期(默认为 90 天)或将密码设置为永不过期。默认情况下,您的组织的密码设置为永不过期。

管理员可以根据密码指南配置安全密码。为了尽可能保证组织的安全,密码应满足复杂性要求,例如最小 8 个字符的长度,否则无法配置。

为组织中的用户设置密码策略非常重要。您始终可以批量重置一个或多个用户密码。

我们将向您展示如何在 Microsoft 365 管理中心和使用 Microsoft Graph PowerShell 管理用户密码。

注意:我们建议在应用任何更改之前和之后运行导出 Microsoft 365 用户密码报告并检查每个用户的密码详细信息。

连接到 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 PowerShell。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Group.ReadWrite.All", "Directory.ReadWrite.All", "Directory.AccessAsUser.All"

现在,您已准备好使用 Microsoft Graph PowerShell 命令。

1.设置域的密码过期策略为永不过期

我们将通过以下方式将组织中每个人的密码过期策略设置为永不过期:

  • Microsoft 365 管理中心
  • 微软图形PowerShell

在 Microsoft 365 管理中心将密码过期策略设置为永不过期

若要在 Microsoft 365 管理中心将密码设置为永不过期,请按照以下步骤操作:

  1. 登录 Microsoft 365 管理中心
  2. 展开设置并点击组织设置
  3. 单击安全和隐私选项卡
  4. 从列表中选择密码过期策略
  5. 选中复选框将密码设置为永不过期(推荐)
  6. 点击保存

[玩转系统] 管理 Microsoft 365 用户密码

您已在 Microsoft 365 管理中心为您的组织设置密码永不过期。接下来,我们将向您展示如何使用 PowerShell 管理 Microsoft 365 用户密码。

使用 PowerShell 将密码过期策略设置为永不过期

您可以将整个域的密码设置为永不过期。

运行以下 PowerShell 命令。

Update-MgDomain -DomainId "a-d.site" -PasswordValidityPeriodInDays "2147483647"

注意:它将整个域的密码设置为永不过期,但不会为之前设置密码过期的个人用户。请记住,它不会覆盖这些用户。

2.设置域密码过期策略

您可以使用 PowerShell 将整个域的密码策略设置为过期。为组织中的每个人设置密码过期策略后,您可以随时更改设置。我们将向您展示如何更改密码过期策略并设置以下内容:

  • 密码通知天数
  • 密码有效期

在 Microsoft 365 管理中心将密码过期策略设置为过期

若要在 Microsoft 365 管理中心将密码设置为过期,请按照以下步骤操作:

  1. 登录 Microsoft 365 管理中心
  2. 展开设置并点击组织设置
  3. 单击安全和隐私选项卡
  4. 从列表中选择密码过期策略
  5. 清除复选框将密码设置为永不过期(推荐)
  6. 输入14 到 730 天之间的数字
  7. 点击保存

[玩转系统] 管理 Microsoft 365 用户密码

将 Microsoft 365 域的密码设置为过期

您可以使用 PowerShell 将整个域的密码设置为过期。我们将添加参数来设置密码通知天数有效期天

如果您已将域的密码策略设置为过期,则可以更改密码过期天数。

-PasswordNotificationWindowInDays

指定用户收到密码即将过期通知之前的天数。如果未设置该属性,则将使用默认值 14 天。

-PasswordValidityPeriodInDays

指定必须更改密码之前的有效时间长度。如果未设置该属性,则将使用默认值 90 天。

我们会将整个域的密码设置为过期。唯一的区别是它不会经过每个用户。因此,如果某些用户已经将密码设置为永不过期,则不会为这些用户更改密码。

请参阅 PowerShell 命令语法。

Update-MgDomain -DomainId "Domain ID" -PasswordNotificationWindowInDays "Number Of Days" -PasswordValidityPeriodInDays "Number Of Days"

运行以下 PowerShell 命令示例。

Update-MgDomain -DomainId "a-d.site" -PasswordNotificationWindowInDays "30" -PasswordValidityPeriodInDays "180"

它将为整个域设置密码过期,并设置密码通知天 (30) 和有效期 (180)。

注意:它将整个域的密码设置为过期,但如果单个用户已经将密码设置为永不过期,则不会覆盖该用户。

3.为用户设置密码永不过期

我们将向您展示如何将以下密码设置为永不过期

  1. 使用 PowerShell 的单个 Microsoft 365 用户
  2. 使用 PowerShell 批量处理所有 Microsoft 365 用户

将单个 Microsoft 365 用户的密码设置为永不过期

我们将为特定 Microsoft 365 用户 [email protected] 设置一个永不过期的密码。

您可以使用UserPrincipalName用户ID号码。

请参阅下面的 PowerShell 命令语法。

Update-MgUser -UserId "UPN or User ID" -PasswordPolicies DisablePasswordExpiration

运行 PowerShell 命令示例。

Update-MgUser -UserId "d912b0fc-6f7e-4ec2-a9e4-854ed27a511a" -PasswordPolicies DisablePasswordExpiration

它将单个用户的密码设置为永不过期

为所有 Microsoft 365 用户批量设置密码永不过期

为所有 Microsoft 365 用户批量设置密码永不过期。这是默认选项,也是 Microsoft 推荐的。

运行以下 PowerShell 脚本。

# Fetch all Azure AD users
$users = Get-MgUser -All

foreach ($user in $users) {
    $UserId = $user.Id
    $DisplayName = $user.DisplayName

    Write-Progress -Activity "Setting password to never expire for $DisplayName" -Status "Processing $DisplayName"

    try {
        # Update user to enable password expiration
        Update-MgUser -UserId $UserId -PasswordPolicies DisablePasswordExpiration -ErrorAction Stop

        Write-Host "Password to never expire set for $DisplayName" -ForegroundColor Green
    }
    catch {
        Write-Host "Can't set password to never expire for $DisplayName" -ForegroundColor Red
    }
}

它将为组织中的所有 Microsoft 365 用户设置密码永不过期

4.为用户设置密码过期

我们将向您展示如何为单个 Microsoft 365 用户和所有 Microsoft 365 用户设置密码过期

  1. 使用 PowerShell 的单个 Microsoft 365 用户
  2. 使用 PowerShell 批量处理所有 Microsoft 365 用户

将单个 Microsoft 365 用户的密码设置为过期

我们将为特定 Microsoft 365 用户 [email protected] 设置密码过期

您可以使用UserPrincipalName用户ID号码。

请参阅下面的 PowerShell 命令语法。

Update-MgUser -UserId "UPN or User ID" -PasswordPolicies None

运行 PowerShell 命令示例。

Update-MgUser -UserId "d912b0fc-6f7e-4ec2-a9e4-854ed27a511a" -PasswordPolicies None

它将为单个用户设置密码过期,默认值为通知 (14) 天和过期 (90) 天。

为所有 Microsoft 365 用户批量设置密码过期

我们将为所有 Microsoft 365 用户批量设置密码过期

运行以下 PowerShell 脚本。

# Fetch all Azure AD users
$users = Get-MgUser -All

foreach ($user in $users) {
    $UserId = $user.Id
    $DisplayName = $user.DisplayName

    Write-Progress -Activity "Set password to expire for $DisplayName" -Status "Processing $DisplayName"

    try {
        # Update user to disable password expiration
        Update-MgUser -UserId $UserId -PasswordPolicies None -ErrorAction Stop

        Write-Host "Set password to expire set for $DisplayName" -ForegroundColor Green
    }
    catch {
        Write-Host "Can't set password to expire for $DisplayName" -ForegroundColor Red
    }
}

您为组织中的所有 Microsoft 365 用户设置密码过期

注意:它将为组织中的每个用户设置密码过期,即使单个用户之前已将密码设置为永不过期。请记住,它将覆盖所有用户。

5.为用户设置预定义密码

我们将向您展示如何设置预定义密码:

  • 单个 Microsoft 365 用户
  • 批量处理所有 Microsoft 365 用户
  • 多个 Microsoft 365 用户 (CSV)

使用预定义密码创建新的 Microsoft 365 用户

您可以创建一个新用户并设置预定义的密码。

在脚本中提供以下信息以创建新用户:

  1. 第 3 行中设置密码
  2. 将第 第 4 行 中的 ForceChangePasswordNextSignIn 设置为 truefalse
  3. 将第 第 5 行 中的 ForceChangePasswordNextSignInWithMfa 设置为 truefalse
  4. 在第 第 10 行 中创建 DisplayName
  5. 在第 11 行 中创建 MailNickName
  6. 在第 第 12 行 中创建 UserPrincipalName

运行以下 PowerShell 脚本。

# Create password profile
$PasswordProfile = @{
    Password                             = "xWwvJ]6NMw+bWH-d"
    ForceChangePasswordNextSignIn        = $true
    ForceChangePasswordNextSignInWithMfa = $true
}

# Create Azure AD user
$AzureUser = @{
    DisplayName       = "Carol Stark"
    MailNickName      = "Carol.Stark"
    UserPrincipalName = "[email protected]"
    PasswordProfile   = $PasswordProfile
    AccountEnabled    = $true
}

New-MgUser @AzureUser

它将使用您提供的上述信息创建一个新用户。

为单个 Microsoft 365 用户设置预定义密码

我们想要为单个 Microsoft 365 用户设置预定义密码。在我们的示例中,我们将为用户 [email protected] 提供一个新密码。用户应使用新的预定义密码登录。

注意:创建预定义密码时,应满足复杂性要求,否则无法配置。

您可以决定用户是否需要更改密码。

注意:如果您希望用户在下次登录时更改密码,请将 $ForceChangePasswordNextSignIn 设置为 true。如果您设置 $ForceChangePasswordNextSignIn >$ForceChangePasswordNextSignIn 为 false,用户不需要更改给定的密码。

  1. 更改第 number 1 行中的 UserId
  2. 将第 第 5 行 中的 ForceChangePasswordNextSignIn 设置为 truefalse
  3. 更改第第 6 行中的密码
  4. 运行以下 PowerShell 脚本
$UserId = "[email protected]"

$params = @{
    passwordProfile = @{
        forceChangePasswordNextSignIn = $false
        password                      = "c*$kJ4!Q2nsQPP"
    }
}

Update-MgUser -UserId $userId -BodyParameter $params

在我们的示例中,用户 Brenda 必须使用预定义的密码,但在下次登录时不得更改密码。

强制更改单个 Microsoft 365 用户的密码

您还可以让用户在下次登录时更改密码,而无需设置预定义密码。因此,用户可以输入当前密码,但需要立即将其更改为新密码。

运行以下脚本以在用户下次登录时强制更改密码。

$userId = "[email protected]"

$params = @{
	passwordProfile = @{
		forceChangePasswordNextSignIn = $true
	}
}

Update-MgUser -UserId $userId -BodyParameter $params

下次 Brenda 使用当前密码登录时,将出现以下窗口。用户被迫更改为新密码。

[玩转系统] 管理 Microsoft 365 用户密码

为所有 Microsoft 365 用户批量设置预定义密码

我们希望为所有 Microsoft 365 用户设置预定义密码。所有用户将获得相同的预定义密码,并且需要使用相同的新密码才能登录。

注意:创建预定义密码时,应满足复杂性要求,否则无法配置。

您可以决定是否强制用户在下次登录后更改密码。如果您将$ForceChangePasswordNectSignIn设置为true,则用户必须更改预定义密码。

  1. 更改第第 5 行中的密码
  2. $ForceChangePasswordNectSignIn设置为truefalse
  3. 运行以下 PowerShell 脚本
# Retrieve a list of all users
$allUsers = Get-MgUser -All

# Define the new password
$newPassword = "Welcome12!"

# Define force password change after sign in
$ForceChangePasswordNextSignIn = "true" #or "false"

# Loop through each user and update their password
foreach ($user in $allUsers) {
    $UserPrincipalName = $user.UserPrincipalName
    $DisplayName = $user.DisplayName

    try {
        Update-MgUser -UserId $UserPrincipalName `
            -PasswordProfile @{
            password                      = $newPassword;
            ForceChangePasswordNextSignIn = $ForceChangePasswordNextSignIn
        } -ErrorAction Stop

        Write-Host "Password updated for user: $userPrincipalName" -ForegroundColor Green
    }
    catch {
        Write-Host "Failed to update password for user: $DisplayName ($UserId)" $_.Exception.Message -ForegroundColor Red
    }
}

从 CSV 为 Microsoft 365 用户设置相同的预定义密码

您可以通过 CSV 文件为多个 Microsoft 365 用户设置相同的预定义密码。我们将向您展示如何导出包含所有用户的 CSV 文件。然后,您可以调整 CSV 文件并仅保留您想要设置预定义密码的用户。

第 1 步:导出 Microsoft 365 用户帐户

请参阅下面的 PowerShell 命令语法。

Get-MgUser -All | Select UserPrincipalName | Export-Csv "path" -NoTypeInformation -Encoding UTF8

运行 PowerShell 命令示例。

Get-MgUser -All | Select UserPrincipalName | Export-Csv "C:\temp\m365users.csv" -NoTypeInformation -Encoding UTF8

它将导出以下带有 UserPrincipalName 的 CSV 文件示例。您可以调整 CSV 文件并删除您不想更改密码的用户。

[玩转系统] 管理 Microsoft 365 用户密码

第 2 步:保存 CSV 文件

将文件命名为 m365users 并将其作为 CSV 文件保存在 temp 文件夹中(如果您的 ( C:) 驱动器。

为了确保 PowerShell 可以读取该文件,请运行 Import-Csv cmdlet。

Import-Csv "C:\temp\m365users.csv"

第 3 步:在脚本中设置相同的预定义密码

您需要为所有用户设置相同的预定义密码,并指定是否希望他们更改密码。

  1. 在第第 8 行中输入密码
  2. 将第 number 11 行中的 ForceChangePasswordNextSignIn 设置为 truefalse

运行以下 PowerShell 脚本。

# Define the path to the CSV file
$csvFilePath = "C:\temp\m365users.csv"

# Load the CSV data into a variable
$csvData = Import-Csv -Path $csvFilePath

# Define the new password
$newPassword = "P@ss8739!7382!"

# Define force password change after sign in
$ForceChangePasswordNextSignIn = "true" #or "false"

# Loop through each user in the CSV data and update their password
foreach ($user in $csvData) {
    $userPrincipalName = $user.UserPrincipalName

    # Check if the user exists
    $existingUser = Get-MgUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

    if ($existingUser -ne $null) {
        try {
            Update-MgUser -UserId $userPrincipalName `
                -PasswordProfile @{
                password                      = $newPassword;
                ForceChangePasswordNextSignIn = $ForceChangePasswordNextSignIn
            } -ErrorAction Stop

            Write-Host "Password updated for user: $userPrincipalName" -ForegroundColor Green
        }
        catch {
            Write-Host "Failed to update password for user: $userPrincipalName" $_.Exception.Message -ForegroundColor Red
        }
    }
    else {
        Write-Host "User not found: $userPrincipalName" -ForegroundColor Yellow
    }
}

从 CSV 为 Microsoft 365 用户设置预定义密码

您还可以通过 CSV 文件为每个 Microsoft 365 用户设置预定义的唯一密码。它将使用您选择的唯一密码更改这些用户的密码。

为每个用户创建一个包含预定义密码的 CSV 文件:

  1. 打开 Microsoft Excel
  2. 在第一列中输入 UserPrincipalName
  3. 列出用户主体名称
  4. 在第二列顶部输入密码
  5. 列出唯一的密码

[玩转系统] 管理 Microsoft 365 用户密码

  1. 将文件命名为 m365usersPass 并将其另存为 CSV 文件
  2. 创建一个临时文件夹并将其保存在 C:\temp
  3. 为了确保 PowerShell 可以读取该文件,请运行 Import-Csv cmdlet
Import-Csv "C:\temp\m365usersPass.csv"

在运行脚本之前,您需要指定是否希望用户在下次登录时更改密码。

  1. 将第 第 8 行 中的 ForceChangePasswordNextSignIn 设置为 truefalse

运行以下 PowerShell 脚本。

# Define the path to the CSV file
$csvFilePath = "C:\temp\m365usersPass.csv"

# Load the CSV data into a variable
$csvData = Import-Csv -Path $csvFilePath

# Define force password change after sign in
$ForceChangePasswordNextSignIn = "true" #or "false"

# Loop through each user in the CSV data and update their password
foreach ($user in $csvData) {
    $userPrincipalName = $user.UserPrincipalName
    $userPassword = $user.Password

    # Check if the user exists
    $existingUser = Get-MgUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

    if ($existingUser -ne $null) {
        try {
            Update-MgUser -UserId $userPrincipalName `
                -PasswordProfile @{
                password                      = $userPassword;
                ForceChangePasswordNextSignIn = $ForceChangePasswordNextSignIn
            } -ErrorAction Stop

            Write-Host "Password updated for user: $userPrincipalName" -ForegroundColor Green
        }
        catch {
            Write-Host "Failed to update password for user: $userPrincipalName" $_.Exception.Message -ForegroundColor Red
        }
    }
    else {
        Write-Host "User not found: $userPrincipalName" -ForegroundColor Yellow
    }
}

6. Microsoft 365用户的导出密码永不过期

您可以检查组织中 Microsoft 365 用户的密码是否设置为永不过期。我们将向您展示如何获取以下密码信息:

  • 单个 Microsoft 365 用户
  • 批量处理所有 Microsoft 365 用户

获取单个 Microsoft 365 用户的密码永不过期

首先,我们想要查看单个 Microsoft 365 用户的密码是否设置为永不过期。我们需要将 Get-MgUser cmdlet 与 UserPrincipalName用户 ID 结合使用。

请参阅下面的 PowerShell 语法示例。

Get-MgUser -UserId "UPN or user ID" -Property UserPrincipalName, PasswordPolicies | Select-Object UserPrincipalName, @{
    N = "PasswordNeverExpires";
    E = { $_.PasswordPolicies -contains "DisablePasswordExpiration" }
}

运行 PowerShell 命令示例。

Get-MgUser -UserId "[email protected]" -Property UserPrincipalName, PasswordPolicies | Select-Object UserPrincipalName, @{
    N = "PasswordNeverExpires";
    E = { $_.PasswordPolicies -contains "DisablePasswordExpiration" }
}

请参阅下面的 PowerShell 输出示例。

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

上面的示例显示密码永不过期为False(默认情况下)。这意味着用户 Brenda 的密码已过期。

为所有 Microsoft 365 用户获取密码永不过期

获取组织中所有 Microsoft 365 用户的密码永不过期信息。

运行以下 PowerShell 命令。

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

请参阅下面的 PowerShell 输出。

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

将密码永不过期报告导出为 CSV

您还可以将所有 Microsoft 365 用户密码永不过期设置批量保存到 CSV 文件中。

如果您没有,请在(C:)驱动器中创建一个temp文件夹。

运行以下 PowerShell 脚本。

Get-MgUser -All -Property UserPrincipalName, PasswordPolicies | 
Select-Object UserPrincipalName, @{ 
    N = "PasswordNeverExpires"; 
    E = { $_.'PasswordPolicies' -contains "DisablePasswordExpiration" } 
} | 
Export-Csv -Path "C:\temp\PasswordReport.csv" -NoTypeInformation -Encoding UTF8

它将导出所有 Microsoft 365 用户密码永不过期设置。在我们的示例中,所有用户的值均为 FALSE,这意味着他们没有“密码永不过期”选项。

[玩转系统] 管理 Microsoft 365 用户密码

显示信息 Microsoft 365 密码策略

请参阅下面的 PowerShell 命令语法。

Get-MgDomain -DomainId "Domain Name" | Ft PasswordNotificationWindowInDays, PasswordValidityPeriodInDays

运行 PowerShell 命令示例。

Get-MgDomain -DomainId "a-d.site" | Ft PasswordNotificationWindowInDays, PasswordValidityPeriodInDays

PowerShell 输出显示以下结果示例。

PS C:\> Get-MgDomain -DomainId "a-d.site" | Ft PasswordNotificationWindowInDays, PasswordValidityPeriodInDays

PasswordNotificationWindowInDays PasswordValidityPeriodInDays
-------------------------------- ----------------------------
                              30                          180

您了解了如何使用 MS Graph PowerShell 管理 Microsoft 365 用户密码。

了解更多:导出所有 Microsoft 365 用户 MFA 状态报告 »

结论

您了解了如何使用 PowerShell 管理 Microsoft 365 用户密码。 Microsoft 建议将整个组织的密码设置为永不过期。与 Microsoft 365 管理中心相比,PowerShell 需要配置更多的密码设置。

您喜欢这篇文章吗?您可能还喜欢在 Microsoft Entra ID 中创建无限的客户端密钥。不要忘记关注我们并分享这篇文章。

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

取消回复欢迎 发表评论:

关灯