0% found this document useful (0 votes)
7 views

Password Reset Script.ps1

Uploaded by

travelwithvipan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Password Reset Script.ps1

Uploaded by

travelwithvipan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Define the input and output file paths

$inputFile = "C:\Usernames.txt" # Path to the text file containing usernames


$outputFile = "C:\PasswordResetResults.csv" # Path to the output CSV file

# Function to generate a complex password


function Generate-ComplexPassword {
$length = 12
$lowercase = 97..122 | ForEach-Object {[char]$_} # Lowercase letters
$uppercase = 65..90 | ForEach-Object {[char]$_} # Uppercase letters
$digits = 48..57 | ForEach-Object {[char]$_} # Digits
$specialChars = 33..47 + 58..64 + 91..96 + 123..126 | ForEach-Object {[char]$_}
# Special characters

# Combine all character sets


$allChars = $lowercase + $uppercase + $digits + $specialChars

# Create a random password


$passwordChars = @(
(Get-Random -InputObject $lowercase) +
(Get-Random -InputObject $uppercase) +
(Get-Random -InputObject $digits) +
(Get-Random -InputObject $specialChars) +
(Get-Random -InputObject $allChars -Count ($length - 4))
)

# Shuffle the password characters and convert to a string


return -join (Get-Random -InputObject $passwordChars)
}

# Initialize an array to hold results


$results = @()

# Import usernames from the text file


if (-Not (Test-Path $inputFile)) {
Write-Host "Input file not found: $inputFile"
exit
}

$usernames = Get-Content -Path $inputFile | Where-Object { $_ -and $_.Trim() -ne ""


}

if ($usernames.Count -eq 0) {
Write-Host "No valid usernames found in the input file."
exit
}

foreach ($username in $usernames) {


try {
# Generate a new complex password
$newPassword = Generate-ComplexPassword

# Reset the user's password in the domain


$user = Get-ADUser -Identity $username -ErrorAction Stop
$user | Set-ADAccountPassword -NewPassword (ConvertTo-SecureString -
AsPlainText $newPassword -Force)

# Add the result to the array


$results += [PSCustomObject]@{
Username = $username
NewPassword = $newPassword
Status = "Success"
}
} catch {
# Handle errors
$results += [PSCustomObject]@{
Username = $username
NewPassword = "N/A"
Status = "Error: $_"
}
}
}

# Export the results to a CSV file


$results | Export-Csv -Path $outputFile -NoTypeInformation -Encoding UTF8

# Inform the user


Write-Host "Password reset process completed. Results are located at: $outputFile"

You might also like