PowerShell Random Password Generator
On a project earlier this year, I had to create random passwords for user accounts as part of a provisioning tool. Perpetually trying to find the fastest way to do something, I came up with a one-liner that you can use to create a random text string from the following ASCII printable characters:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~0123456789
To create the passwords, I use this bit of magic:
PS> $Password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..9 | sort {Get-Random})[0..8] -join ''
PS> $Password
Z-=$fNgb!
That will create an 9 character password using the range operator [0..8]. And, if you want to concatenate it with a plaintext counterpart:
PS> $Password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..9 | sort {Get-Random})[0..8] -join ''
PS> $Password = "Welcome"+$Password
PS> $Password
WelcomeZ-=$fNgb!
Maybe not the most difficult passwords in the world, but probably good enough to give new users the first time they log on to a system.
But then, I found this gem, too:
$Password = "Welcome" + [system.web.security.membership]::GeneratePassword(x,y)
Where:
x = length in characters
y = minimum number of non-alphanumeric characters
If you only want alpha-numeric characters, simply use "x." As soon as you use "y", you'll get anywhere from y to x number of non-alphanumeric characters.
What ideas do you have?