-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangePasswords.ps1
More file actions
37 lines (32 loc) · 1.64 KB
/
changePasswords.ps1
File metadata and controls
37 lines (32 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function ChangeEmployeePasswords{
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[string]$UserFilePath,
[Parameter(Mandatory)]
[string]$PasswordFilePath
)
try {
Write-Host "Loading users from '$UserFilePath'..." -ForegroundColor Yellow
$UsersToReset = Import-Csv -Path $UserFilePath -ErrorAction Stop
Write-Host "Loading passwords from '$PasswordFilePath'..." -ForegroundColor Yellow
$NewPasswords = Import-Csv -Path $PasswordFilePath -ErrorAction Stop
if ($UsersToReset.Count -ne $NewPasswords.Count) {
throw "Error not equal number of users and new passwords."
}
for ($i = 0;$i -lt $UsersToReset.Count; $i++){
$employee = $UsersToReset[$i]
$newPassword = $NewPasswords[$i]
$samAccountName = $employee.MatchedSamAccountNames.Trim()
$adUser = Get-ADUser -Filter "SamAccountName -eq '$escapedSamAccountName'" -ErrorAction Stop
$escapedSamAccountName = $samAccountName.replace("'","''")
$securePassword = ConvertTo-SecureString $newPassword.Password -AsPlainText -Force
Write-Host "Resetting password for user: $($adUser.SamAccountName)..."
Set-ADAccountPassword -Identity $adUser -NewPassword $securePassword -Reset
Write-Host "Successfully reset password for '$($adUser.SamAccountName)'." -ForegroundColor Green
}
} catch {
Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
}
}
ChangeEmployeePasswords -UserFilePath "getSamAccountNames_Data.csv" -PasswordFilePath "newPasswords.csv"