As part of the auto-provisioning system I am working on I need something to generate secure passwords.
This function will return a random password of any length you pass it, based on an ASCII character set you can define in the script, or you can pass it nothing and it will make you a 10 character password.
The variable $ASCII is your character set, and you can just keep adding to it like I have bellow. I find that passwords with lots of special characters are impossible to type over a remote console so I have purposely kept the character set restricted, but you might not want that.
Here are the ASCII codes : https://www.petefreitag.com/cheatsheets/ascii-codes/
Here is the function :
Function GET-Randompassword() {
Param(
[int]$length=10
)
$ascii=$NULL
For ($a=65;$a –le 90;$a++) {$ascii+=,[char][byte]$a } #Upper case letters
For ($a=97;$a –le 122;$a++) {$ascii+=,[char][byte]$a } #Lower case letters
For ($a=48;$a –le 57;$a++) {$ascii+=,[char][byte]$a } #Number
For ($a=33;$a –le 38;$a++) {$ascii+=,[char][byte]$a } #Some special characters
For ($loop=1; $loop –le $length; $loop++)
{
$TempPassword+=($ascii| GET-RANDOM)
}
return $TempPassword
}
Enjoy