建议不要强迫用户按时间表更改密码,除非有证据违反了安全性。但是,有时您想检查用户上次更改密码何时。在本文中,您将学习如何使用Microsoft Graph PowerShell导出Microsoft 365用户最后一个密码更改日期和时间。
安装Microsoft Graph PowerShell
以管理员的身份运行PowerShell并安装Microsoft Graph PowerShell模块。
Install-Module Microsoft.Graph -Force
重要的:在运行CMDLET或脚本以防止错误和错误结果之前,请务必更新到最新的Microsoft Graph PowerShell模块版本。
连接到Microsoft Graph PowerShell
您需要使用正确的权限连接到Microsoft Graph PowerShell。
Connect-MgGraph -Scopes "User.Read.All"
您是否要在没有用户交互的情况下连接,因为您希望脚本自动运行?用基于证书的身份验证或客户秘密设置它。在文章中阅读更多信息,请连接到Microsoft Graph PowerShell。
要使所有Microsoft 365用户使用最后一个密码更改日期和时间,请在下面运行命令。
Get-MgUser -All -Property DisplayName, UserPrincipalName, LastPasswordChangeDateTime | Select-Object -Property DisplayName, UserPrincipalName, LastPasswordChangeDateTime
出现输出。
DisplayName UserPrincipalName LastPasswordChangeDateTime
----------- ----------------- --------------------------
Alison Bell 16/04/2024 17:57:46
Amanda Morgan 04/06/2024 17:39:05
Audrey Graham 11/10/2024 11:12:34
Boris Campbell 11/12/2024 17:12:17
虽然输出很棒,但我们希望有关每个Microsoft 365用户的更多信息。让我们创建一个报告以检索更多属性并将其导出到CSV文件。
导出Microsoft 365用户最后一个密码更改报告使用PowerShell
Export-M365lastPasschange.ps1 PowerShell脚本将在Microsoft 365/Entra租户中获取所有用户及其最后一个密码更改日期和时间。之后,它将报告将报告导出到CSV文件。您可以使用Microsoft Excel或任何其他支持CSV文件扩展名的应用程序打开CSV文件。
脚本将收集以下信息:
- ID
- 用户principalname
- 邮件
- DisplayName
- 密码
- LastPasswordChangeDateTime
- CreateDateTime
- 领域
- MaxPasswordage
- 密码
- 到期
- 日子
要用PowerShell导出Microsoft 365中的所有最后一个密码更改,请按照以下步骤:
步骤1。下载导出M365lastPasschange PowerShell脚本
在(c :)驾驶:
- 脚本
- 温度
下载并将Export-M365lastPasschange.ps1 PowerShell脚本放在C:脚本文件夹。该脚本将以其最后一个密码更改日期和时间向所有用户导出到报告中的报告C:温度文件夹。
确保文件未阻止以防止运行脚本时的错误。在运行PowerShell脚本时,请在文章中阅读更多信息。
另一个选择是将下面的代码复制并粘贴到记事本中。给它名字导出M365lastPasschange.ps1并将其放在C:脚本文件夹。
<#
.SYNOPSIS
.Export-M365LastPassChange.ps1
.DESCRIPTION
The script exports the last password change date and time, and more properties
for all Microsoft 365 users to a CSV file.
.LINK
www.alitajran.com/export-microsoft-365-users-last-password-change-date-and-time/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
X: x.com/alitajran
.CHANGELOG
V1.00, 12/23/2024 - Initial version
#>
param (
[Parameter(Mandatory = $true)]
[string]$ExportPath
)
# Connect to Microsoft Graph PowerShell
Connect-MgGraph -Scopes "User.Read.All", "Domain.Read.All" -NoWelcome
# Define CSV file export location variable using the parameter provided
$Csvfile = "$ExportPath"
# Retrieve all domain password policies
$domains = Get-MgDomain | Select-Object Id, PasswordValidityPeriodInDays
# Define the properties to retrieve for users
$properties = @(
"Id",
"UserPrincipalName",
"mail",
"displayName",
"PasswordPolicies",
"LastPasswordChangeDateTime",
"CreatedDateTime"
)
# Splat the user parameters
$userParams = @{
Filter = "userType eq 'member' and accountEnabled eq true"
Property = $properties
CountVariable = 'userCount'
ConsistencyLevel = 'Eventual'
All = $true
Verbose = $true
}
# Fetch user information
$users = Get-MgUser @userParams | Select-Object $properties
# Get the current datetime for calculation
$timeNow = Get-Date
# Initialize a List to store user information
$Report = [System.Collections.Generic.List[Object]]::new()
# Process users and gather required data
foreach ($user in $users) {
$userDomain = ($user.userPrincipalName).Split('@')[1]
$maxPasswordAge = ($domains | Where-Object { $_.Id -eq $userDomain }).PasswordValidityPeriodInDays
if ($user.PasswordPolicies -contains "DisablePasswordExpiration" -or $maxPasswordAge -eq 2147483647) {
$ReportLine = [PSCustomObject][Ordered]@{
Id = $user.Id
UserPrincipalName = $user.UserPrincipalName
Mail = $user.mail
DisplayName = $user.DisplayName
PasswordPolicies = $user.PasswordPolicies -join "; "
LastPasswordChangeDateTime = $user.LastPasswordChangeDateTime
CreatedDateTime = $user.CreatedDateTime
Domain = $userDomain
MaxPasswordAge = "Password does not expire"
PasswordAge = (New-TimeSpan -Start $user.LastPasswordChangeDateTime -End $timeNow).Days
ExpiresOn = "N/A"
DaysRemaining = "N/A"
}
# Add the report line to the List
$Report.Add($ReportLine)
}
else {
$expiresOn = $user.LastPasswordChangeDateTime.AddDays($maxPasswordAge)
$daysRemaining = (New-TimeSpan -Start $timeNow -End $expiresOn).Days
$ReportLine = [PSCustomObject][Ordered]@{
Id = $user.Id
UserPrincipalName = $user.UserPrincipalName
Mail = $user.mail
DisplayName = $user.DisplayName
PasswordPolicies = $user.PasswordPolicies -join "; "
LastPasswordChangeDateTime = $user.LastPasswordChangeDateTime
CreatedDateTime = $user.CreatedDateTime
Domain = $userDomain
MaxPasswordAge = $maxPasswordAge
PasswordAge = (New-TimeSpan -Start $user.LastPasswordChangeDateTime -End $timeNow).Days
ExpiresOn = $expiresOn
DaysRemaining = if ($daysRemaining -le 0) { "Expired" } else { $daysRemaining }
}
# Add the report line to the List
$Report.Add($ReportLine)
}
}
# Sort on UserPrincipalName
$Report = $Report | Sort-Object UserPrincipalName
# Display the results in Out-GridView
$Report | Out-GridView -Title "Microsoft 365 Users Last Password Change Date and Time Report"
# Export results to CSV
$Report | Export-Csv -Path $Csvfile -NoTypeInformation -Encoding utf8
Write-Host "Script completed. Report exported to $Csvfile" -ForegroundColor Cyan
步骤2。运行导出M365lastPasschange PowerShell脚本
在下面运行命令以运行导出M365lastPasschange.ps1PowerShell脚本并创建报告。
C:scripts.Export-M365LastPassChange.ps1 -ExportPath "C:tempLastPassChangeReport.csv"
报告输出将发送到单独的窗口中的交互式表(Out-GridView)。

步骤3。检查用户最后一个密码更改日期和时间报告CSV文件
Export-M365lastPasschange.ps1 PowerShell脚本将所有用户带有最后一个密码将日期和时间更改为CSV文件。
查找文件lastpasschangereport.csv在路径中C:温度。
使用您喜欢的应用程序打开CSV文件。在我们的示例中,这是Microsoft Excel。
阅读更多:


就是这样!
结论
您学会了如何导出Microsoft 365用户最后一个密码更改日期和时间。下次您想强迫用户更改密码并想知道状态时,您可以运行脚本并检查日期和时间。最好有一个脚本,可以创建一个包含您需要的所有信息的报告。
您喜欢这篇文章吗?您可能还喜欢为本地配置Microsoft Entra密码保护。不要忘记关注我们并分享这篇文章。