The PowerShell script given below can be used to automatically reset the passwords at regular intervals. ADSelfService Plus also offers an option that can be used to automatically reset domain user’s passwords when they expire. When this option is enabled, a scheduler runs at regular intervals to search for password expired user accounts and automatically resets the passwords. The new password is then mailed to the user. Here is a comparison between the automatic password reset using PowerShell and ADSelfService Plus:
Param (
[Parameter(Mandatory=$True)]
[String]$InputFile
) Function MakeRandomPass {
Param (
[Int]$PLength
)
If ($PLength -LT 4) {Return $Null}
$Numbers = $Null
For ($A=48;$A -LE 57;$A++) {$Numbers+=,[Char][Byte]$A}
$UpCase = $Null
For ($A=65;$A -LE 90;$A++) {$UpCase+=,[Char][Byte]$A}
$LowCase = $Null
For ($A=97;$A -LE 122;$A++) {$LowCase+=,[Char][Byte]$A}
$SpChar = $Null
For ($A=33;$A -LE 47;$A++) {$SpChar+=,[Char][Byte]$A}
For ($A=58;$A -LE 64;$A++) {$SpChar+=,[Char][Byte]$A}
For ($A=123;$A -LE 126;$A++) {$SpChar+=,[Char][Byte]$A}
$Buffer = @()
For ($A=1;$A -LE $PLength;$A++) {$Buffer+=0}
While ($True) {
$NumChar = (Get-Random -Minimum 0 -Maximum $PLength)
If ($Buffer[$NumChar] -EQ 0) {$Buffer[$NumChar] = 1; break}
}
While ($True) {
$NumChar = (Get-Random -Minimum 0 -Maximum $PLength)
If ($Buffer[$NumChar] -EQ 0) {$Buffer[$NumChar] = 2; break}
}
While ($True) {
$NumChar = (Get-Random -Minimum 0 -Maximum $PLength)
If ($Buffer[$NumChar] -EQ 0) {$Buffer[$NumChar] = 3; break}
}
While ($True) {
$NumChar = (Get-Random -Minimum 0 -Maximum $PLength)
If ($Buffer[$NumChar] -EQ 0) {$Buffer[$NumChar] = 4; break}
}
$ThisPassword = $Null
ForEach ($CharType In $Buffer) {
If ($CharType -EQ 0) {
$CharType = ((1,2,3,4) | Get-Random)
}
Switch ($CharType) {
1 {$ThisPassword+=($Numbers | Get-Random)}
2 {$ThisPassword+=($UpCase | Get-Random)}
3 {$ThisPassword+=($LowCase | Get-Random)}
4 {$ThisPassword+=($SpChar | Get-Random)}
}
}
Return $ThisPassword
}
$ErrorActionPreference = "SilentlyContinue"
$T = Get-Date
If ($Error) {$Error.Clear()}
Write-Host "`n"
Write-Host "Working. Please wait"
Write-Host "`n"
$RepFile = $T -Replace " ", $Null
$RepFile = $RepFile -Replace ":", $Null
$RepFile = $RepFile -Replace "/", $Null
$RepFile = $RepFile -Replace "-", $Null
If (Test-Path "Report_$RepFile.txt") {
Remove-Item "Report_$RepFile.txt"
}
New-Item -Path "Report_$RepFile.txt" -Type File -Force -Value "REPORT: Reset Local User Account Password On Multiple Computers" | Out-Null
Add-Content "Report_$RepFile.txt" "`n"
Add-Content "Report_$RepFile.txt" "`n"
Add-Content "Report_$RepFile.txt" "Report Created On $T"
Add-Content "Report_$RepFile.txt"
Add-Content "Report_$RepFile.txt" "`n"
Import-CSV -Path $InputFile | ForEach-Object {
Try {
$ThisMachine = $_.ComputerName
$ThisAccount = $_.LocalAccountLoginID
If (!([string]::IsNullOrEmpty($ThisMachine)) -AND !([string]::IsNullOrEmpty($ThisAccount))) {
Write-Host "`tAttempting to reset the local account password in computer: $ThisMachine" -ForeGroundColor "Yellow"
$PassToSet = MakeRandomPass 20
$ThisUser = [ADSI]"WinNT://$ThisMachine/$ThisAccount, User"
$ThisUser.SetPassword($PassToSet)
$ThisUser.SetInfo()
If (!$Error) { Add-Content "Report_$RepFile.txt" "$ThisMachine `t`t -- $ThisAccount `t`t -- $PassToSet `t`t --success: Password Has Been Reset/Changed."
}
}
}
Catch {
[System.Exception] | Out-Null
If ($Error) {
Add-Content "Report_$RepFile.txt" "$ThisMachine `t`t -- $ThisAccount `t`t -- Password Reset has failed. An Error Has Occurred."
Add-Content "Report_$RepFile.txt" $Error
$Error.Clear()
}
}
}
Write-Host "`n"
Write-Host "Task Completed. Check Report File: Report_$RepFile.txt"
Notepad "Report_$RepFile.txt"
Write-Host "`n" In ADSelfService Plus:
The script below lists users who haven’t changed passwords in 90 days.
$users = Get-ADUser -Filter {PasswordLastSet -lt (Get-Date).AddDays(-90)} -Properties PasswordLastSetRun the script below to automatically reset passwords for all identified users. This resets passwords to NewPassword123.
foreach ($user in $users) {
$newPassword = ConvertTo-SecureString "NewPassword123!" -AsPlainText -Force
Set-ADAccountPassword -Identity $user.SamAccountName -NewPassword $newPassword -Reset
}Ensure users update their passwords with a prompt to set a new password at the next login.
foreach ($user in $users) {
Set-ADUser -Identity $user.SamAccountName -ChangePasswordAtLogon $true
}Send email alerts to users about the reset.
foreach ($user in $users) {
Send-MailMessage -To $user.EmailAddress -From "admin@yourdomain.com" -Subject "Password Reset" -Body "Your password has been reset. Please update it upon your next login."
}Reset a user's password using the script below. Replace "NewPass@123" with the new password.
Set-ADAccountPassword -Identity username -NewPassword (ConvertTo-SecureString "NewPass@123" -AsPlainText -Force) -ResetForce the user to change their password using the script below.
Set-ADUser -Identity username -ChangePasswordAtLogon $trueYes, use a CSV file and script automation to reset passwords for multiple users in bulk.