[玩转系统] 强制更改 Microsoft 365 中所有用户的密码
作者:精品下载站 日期:2024-12-14 01:57:52 浏览:14 分类:玩电脑
强制更改 Microsoft 365 中所有用户的密码
您可以在 Microsoft 365 管理中心强制更改用户的密码。但是,如果您想为许多用户执行此操作,最好使用 PowerShell。这是因为它比手动检查用户要快得多。在本文中,你将了解如何使用 PowerShell 强制 Microsoft 365 中的所有用户更改密码。
安装 Microsoft Graph PowerShell
以管理员身份运行 Windows PowerShell 并安装 Microsoft Graph PowerShell。
Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force
重要提示:始终安装 Microsoft Graph PowerShell 和 Microsoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。
连接到 Microsoft Graph PowerShell
然后,使用以下范围连接到 Microsoft Graph PowerShell。
Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All", "Directory.AccessAsUser.All"
强制单个用户更改密码
这将在用户使用密码登录后向用户显示更改密码提示。
$userId = "[email protected]"
$params = @{
passwordProfile = @{
forceChangePasswordNextSignIn = $true
}
}
Update-MgUser -UserId $userId -BodyParameter $params
用户使用密码登录后,会出现更新密码提示。
更改密码并强制更改单个用户的密码
这将更改用户的密码。之后,他们需要使用该密码登录。登录后,他们将收到更新密码的提示。
$UserId = "[email protected]"
$params = @{
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = "B%#X*4BUiHK3%3"
}
}
Update-MgUser -UserId $userId -BodyParameter $params
强制所有用户更改密码
强制所有用户在下次登录时更改密码。
重要提示:将管理员 UPN 放在第 2 行上,以便将其从脚本中排除。
$allUsers = Get-MgUser -All
$upnToExclude = "[email protected]"
foreach ($user in $allUsers) {
if ($user.UserPrincipalName -ne $upnToExclude) {
try {
$userId = $user.Id
$params = @{
passwordProfile = @{
forceChangePasswordNextSignIn = $true
}
}
Update-MgUser -UserId $userId -BodyParameter $params
# Log successful update
Write-Host "Successfully updated password profile for user: $($user.UserPrincipalName)" -ForegroundColor Green
}
catch {
# Log error or failure
Write-Host "Failed to update password profile for user: $($user.UserPrincipalName). Error: $_" -ForegroundColor Red
}
}
else {
Write-Host "Skipping user with UPN: $upnToExclude" -ForegroundColor Cyan
}
}
更改密码并强制所有用户更改密码
强制更改所有用户的密码。使用新密码登录后,他们将收到密码提示以更改密码。
PowerShell 脚本将为每个用户创建一个随机密码,并将其导出到 C:\Temp 中的 CSV 文件 PasswordReset.csv。
重要提示:将管理员 UPN 放在第 2 行上,以便将其从脚本中排除。
$allUsers = Get-MgUser -All
$upnToExclude = "[email protected]"
$CSVExportPath = "C:\temp\PasswordReset.csv"
$PasswordLength = 14
# Randomize passwords
function Get-RandomPassword {
Param(
[int]$Length
)
Begin {
if ($Length -lt 4) {
End
}
$Numbers = 1..9
$LettersLower = 'abcdefghijklmnopqrstuvwxyz'.ToCharArray()
$LettersUpper = 'ABCEDEFHIJKLMNOPQRSTUVWXYZ'.ToCharArray()
$Special = '!@#$%^&*()=+[{}]/?<>'.ToCharArray()
# For the 4 character types (upper, lower, numerical, and special)
$N_Count = [math]::Round($Length * .2)
$L_Count = [math]::Round($Length * .4)
$U_Count = [math]::Round($Length * .2)
$S_Count = [math]::Round($Length * .2)
}
Process {
$Pswrd = $LettersLower | Get-Random -Count $L_Count
$Pswrd += $Numbers | Get-Random -Count $N_Count
$Pswrd += $LettersUpper | Get-Random -Count $U_Count
$Pswrd += $Special | Get-Random -Count $S_Count
# If the password length isn't long enough (due to rounding), add X special characters
# Where X is the difference between the desired length and the current length.
if ($Pswrd.length -lt $Length) {
$Pswrd += $Special | Get-Random -Count ($Length - $Pswrd.length)
}
# Grab the $Pswrd string and randomize the order
$Pswrd = ($Pswrd | Get-Random -Count $Length) -join ""
}
End {
$Pswrd
}
}
$Report = [System.Collections.Generic.List[Object]]::new()
foreach ($user in $allUsers) {
if ($user.UserPrincipalName -ne $upnToExclude) {
try {
$userId = $user.Id
# Generate a random password
$newPassword = Get-RandomPassword -Length $PasswordLength
$params = @{
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = $newPassword
}
}
Update-MgUser -UserId $userId -BodyParameter $params
# Log successful update and add to CSV data
Write-Host "Successfully updated password for user: $($user.UserPrincipalName) with new password." -ForegroundColor Green
$ReportLine = [PSCustomObject]@{
UserPrincipalName = $user.UserPrincipalName
NewPassword = $newPassword
}
$Report.Add($ReportLine)
}
catch {
Write-Host "Failed to update password for user: $($user.UserPrincipalName). Error: $_" -ForegroundColor Red
$ReportLine = [PSCustomObject]@{
UserPrincipalName = $user.UserPrincipalName
NewPassword = "Failed to update"
}
$Report.Add($ReportLine)
}
}
else {
Write-Host "Skipping user with UPN: $upnToExclude" -ForegroundColor Cyan
}
}
# Export data to CSV
$Report | Export-Csv "$CSVExportPath" -NoTypeInformation -Encoding utf8
Write-Host "Passwords have been successfully exported to $CSVExportPath" -ForegroundColor Cyan
就是这样!
了解更多:使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户 »
结论
您了解了如何使用 PowerShell 强制更改 Microsoft 365 中所有用户的密码。使用正确的命令和脚本,您可以立即采取行动。
您喜欢这篇文章吗?您可能还喜欢导出 Microsoft 365 非活动用户报告。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag