PowerShell Script to Export Users’ Last Password Reset Dates to CSV

If you need to export the list of users in your Active Directory along with their last password reset or update date, here’s a simple PowerShell script that will help you achieve that. The script retrieves the PasswordLastSet property for all users and exports the results into a CSV file for easy analysis.


PowerShell Script:

# Ensure the Active Directory module is loaded
Import-Module ActiveDirectory

# Define the path for the CSV file where the results will be saved
$csvFilePath = "C:\Users\LastPasswordReset.csv"

# Retrieve all users and export their DisplayName, SamAccountName, and PasswordLastSet
Get-ADUser -Filter * -Properties DisplayName, PasswordLastSet | Select-Object `
DisplayName, SamAccountName, PasswordLastSet | Sort-Object PasswordLastSet |
Export-Csv -Path $csvFilePath -NoTypeInformation

# Output the path of the CSV file
Write-Host "Exported results to $csvFilePath"

How the Script Works:

  1. Imports the Active Directory Module:
    The script starts by ensuring that the Active Directory module is loaded into your PowerShell session.
  2. Defines the Export Path:
    The $csvFilePath variable defines the location where the results will be saved. In this case, it saves to "C:\Users\LastPasswordReset.csv", but you can modify it as per your needs.
  3. Retrieves Users’ Password Information:
    The script fetches all users in Active Directory, along with their DisplayName, SamAccountName (username), and PasswordLastSet properties.
  4. Sorts by Last Password Reset Date:
    The users are sorted based on the PasswordLastSet date to help identify when the last password reset took place.
  5. Exports to CSV:
    Finally, the results are exported to a CSV file, making it easier to analyze and share the data.

Sample Output:

After running the script, you’ll get a CSV file with the following format:

DisplayNameSamAccountNamePasswordLastSet
John Doejohndoe2024-01-20 15:30:00
Jane Smithjanesmith2023-12-10 10:20:00
Michael Johnsonmjohnson2023-11-05 09:15:00

Running the Script:

  1. Save the Script: Save the script as a .ps1 file (e.g., Export-PasswordLastReset.ps1).
  2. Run PowerShell as Administrator: Ensure you have the necessary permissions to query Active Directory and run the script.
  3. Customize the File Path (Optional): You can modify the $csvFilePath variable to save the CSV file to a different location.
  4. Execute the Script: Run the script, and the results will be saved in the defined path.

This script is a great way to quickly audit and review when users last changed their passwords, helping with compliance and security assessments.

Let me know if you have any questions or need further customization!


This can be posted as a helpful guide or a tutorial for others working with Active Directory and PowerShell

Junaid Ahmed
Junaid Ahmed

Junaid Ahmed is a Cloud Infrastructure and Identity Management expert with 10+ years of experience specializing in Azure Entra ID, ADFS, Hybrid Identity, and Azure Infrastructure Management. He has a proven track record of leading secure identity solutions, deploying high-value security projects, and troubleshooting complex Azure issues for global clients. Junaid excels in enhancing system performance, facilitating seamless collaboration across organizations, and delivering expert guidance on cloud migrations and infrastructure optimization. He seeks to leverage his expertise in a challenging Cloud Solution Architect role to drive success through innovative cloud solutions.

Articles: 30

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Appliance - Powered by TurnKey Linux