PowerShell – Generate password from word list and numbers for AD User

I’ve always used a random password generator for some sub accounts that change weekly via task scheduler. The issue with that is the lower case l and 1’s depending on the systems font look very similar resulting in tickets saying the password doesn’t work.

I’ve patched together this script from a few online sources and use this in Adaxes.

This script will:

  • Pull words from $WordList
  • Add a random number to the end of the password between 1000-9999
  • Email the password to the specified email address.
$WordList = 'Ocean','Mountain','Desert','Oregon','Sunbreak','Pacific','Northwest','Timber','Salmon','Cougar','Deer','Turtle'

Function Create-Password
{ 
  # query the smaller list of words for single entry (2 times)
  $word1 = $WordList | Sort-Object {Get-Random} -unique | select -first 1
  $word2 = $WordList | Sort-Object {Get-Random} -unique | select -first 1

  # create random digits
  $number1 = Get-Random -Minimum 1000 -Maximum 9999
  
  # concatenante and return new random password
  return ($word1) + ($word2) + $number1
}

$PasswordGen = Create-Password


$password = "$PasswordGen" # TODO: modify me
$to = "CHANGEME@changeme.com" # TODO: modify me
$subject = "Password Reset for %fullname%" # TODO: modify me
$messageText = @"
<p><span style="font-size:16px;"><span style="font-family:times new roman,times,serif;">Password has been reset for %fullname%.</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Consolas;">New password: $PasswordGen <br />
Username: %username%</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:times new roman,times,serif;">Please do not reply to this e-mail, it has been sent to you for notification purposes only.</span></span></p>
"@ # TODO: modify me

$Context.TargetObject.SetPassword($password)
$Context.SendMail($to, $subject, $NULL, $messageText)

Here is another to just generate a list of passwords.

$rand = new-object System.Random

# read-in very large file creating smaller list of random words
$words = 'Ocean','Mountain','Desert','Oregon','Sunbreak','Pacific','Northwest','Timber','Salmon','Cougar','Deer','Turtle'

Function Create-Password
{ 
  # query the smaller list of words for single entry (2 times)
  $word1 = $words | Sort-Object {Get-Random} -unique | select -first 1  
  $word2 = $words | Sort-Object {Get-Random} -unique | select -first 1  

  # create random digits
  $number1 = Get-Random -Minimum 1000 -Maximum 9999
  
  # concatenante and return new random password
  return (Get-Culture).TextInfo.ToTitleCase($word1) + (Get-Culture).TextInfo.ToTitleCase($word2) + $number1

}

# generate 20 passwords
for($i=1; $i -le 100; $i++){
  Create-Password
}

Leave a Comment

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

Scroll to Top