Here is my take on a Microsoft Powershell password generator function. You can control which characters are used within the $chars variable if you want to change the default. I left out some characters to avoid issues with generated passwords and thus far didn’t run into problems. The code is documented by itself and you can copy and paste it into your own function script or create an independent New-Password.ps1 file.
#requires -version 2 <# .SYNOPSIS Program : Password Generator (New-Password) Author : Eugene Dullaard Date : 23-Dec-2012 - Initial Script Update : 25-May-2013 - Added Regular Expression for checking output (complexity) - Added default length of 8 if no length has been supplied - Throw error if password is not meeting minimal length New-Password generates a password out of predefined characters for a specified length. .DESCRIPTION New-Password generates a password out of predefined characters for a specified length. The generated password is always complex, the minimal lenght is 3 characters, there is no maximum specified. If no value is given it generates an 8 character password. You can modify the script if you want to remove or add characters that can be used, currently confusing characters like lowercase L, capital O and zero are not used. .EXAMPLE New-Password -Length 12 will give you a password with the length of 12 characters. New-Password will give you a password with a default lenght of 8 characters. You need to supply a Length if you desire a password with a different length. .EXAMPLE $Password = New-Password 8 will store an eight character password into the $password variable. #> function New-Password { param ([INT]$Length=8) # $chars contains the variables you would like to use in your password begin {$chars="abcdefghijkmnopqrstuvwxyz*[];,.()<>!-+=_123456789ABCDEFGHJKLMNPQRSTUVWXYZ"} process { # Check length of password if ($Length -le 2) {Throw "Password too short"} # Start Loop to check if password matches complexity do { # Make sure the variable containing the password is empty # in case of failing to meet complexity criteria $RandomPassword = "" # Password creation process using a loop for the given length # adding one character each turn for ($a=1; $a -le $Length; $a++) {$RandomPassword += $chars[(Get-Random -Maximum $chars.Length)]} } until ($randompassword -match "^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s)") } # Return the created password end {return $RandomPassword} }