Active directory password reset by CSV file

As an IT pro with a “functional” PowerShell knowledge, I hunted around a bit online looking for a way to do this. None of the solutions I found were doing what I wanted so I thought I’d share my simple solution. In this case, I was looking for a solution to quickly and easily reset and document bulk passwords for restricted student exam accounts.

Generate some passwords

Use an Excel randomisation formula like the one in this spreadsheet to create passwords of your choice. In this instance we’re using some adjectives, animal types and a two-digit number – easy for students to read and type in for an exam.

Define some accounts/passwords

Again using Excel (if that’s your thing), create a CSV file with the headings samAccountName and password. Enter the usernames of the accounts you’d like to update and copy the passwords you’ve just generated. If in Excel, remember to paste the values, not the formula.

Set some passwords

Copy this code into a new PowerShell script, modify with the path to your CSV file and run.

#ensure AD module is imported so things work
Import-Module ActiveDirectory

#set path to CSV file here:
$filepath = "C:\users.csv"

#for each row in the CSV
$passwordList = Import-CSV $filepath | ForEach-Object {

#retrieve the username and password
$username = $_."samAccountName"
$password = $_."password"

#convert password to secure string
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force

#set new password
Set-ADAccountPassword -Identity $username -NewPassword $securePassword -Reset

#confirm accounts being updated
Write-Host "Password has been set for: "$username

}