Create Complex Random Passwords

by Dec 14, 2010

Here is how you can create random passwords that meet certain requirements:

function Get-RandomPassword {
param(
$length = 10,
$characters =
 'abcdefghkmnprstuvwxyzABCDEFGHKLMNPRSTUVWXYZ123456789!"§$%&/()=?*+#_'
)
# select random characters
$random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
# output random pwd
$private:ofs=""
[String]$characters[$random]
}
function Randomize-Text {
param(
$text
)
$anzahl = $text.length 1
$indizes = Get-Random -InputObject (0..$anzahl) -Count $anzahl

$private:ofs=''
[String]$text[$indizes]
}

function Get-ComplexPassword {
$password = Get-RandomPassword -length 6 -characters 'abcdefghiklmnprstuvwxyz'
$password += Get-RandomPassword -length 2 -characters '#*+)'
$password += Get-RandomPassword -length 2 -characters '123456789'
$password += Get-RandomPassword -length 4 -characters 'ABCDEFGHKLMNPRSTUVWXYZ'

Randomize-Text $password
}

Get-ComplexPassword creates a 14-character password consisting of six lowercase characters, two special characters, two numbers, and four uppercase characters. This is done by creating four parts and then melting them together with Randomize-Text, which takes a text and randomly re-arranges the characters. With this toolset, you can then create almost any random password adhering to almost any complexity rule you are likely to run into.

Twitter This Tip!
ReTweet this Tip!