PowerShell function to generate a random password

This is part of one of my AD migration scripts which I find very useful.
Whenever creating a user, I like to generate a random and complex password. The length of the password differs for each of my customers, so that’s something I define with a parameter added to this function:

Function New-RandomPassword ($Length)
#Usage: New-RandomPassword 8
$Random = New-Object System.Random
$RandomPassword = (-join ([char[]](33..127) | Get-Random -Count $length))
Write-Output $RandomPassword

Note that this one is very basic, no check in there for when both a higher- and lower case character, number and/or punctuation mark is included. For most of my customers the function above is sufficient Knipogende emoticonI Hope you find this one useful Glimlach


Update: Changed the script based on feedback from Shay Levy through Twitter… thanks! 😀



Leave a Reply

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