Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Created July 25, 2016 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dazfuller/9dbb98d2fc163d147068629f35e9a5ae to your computer and use it in GitHub Desktop.
Save dazfuller/9dbb98d2fc163d147068629f35e9a5ae to your computer and use it in GitHub Desktop.
function Get-Password {
Param (
[ValidateRange(8, 20)]
[int] $PasswordLength = 14,
[ValidateRange(1, 4)]
[int] $MinimumSpecialCharacters = 3
)
# Specify the range of ASCII characters to use, avoiding ambiguous characters such as 0, O, o
# also limit the allowed special characters to those which are easy to read
$punc = 33..33 + 35..38 + 42..43 + 45..45
$numbers = 50..57
$letters = 65..78 + 80..90 + 97..110 + 112..122
# Generate the list of required minimum special characters
$password = Get-Random -Count $MinimumSpecialCharacters -InputObject $punc `
|% -Begin { $aa = $null } `
-Process { $aa += ([char]$_) } `
-End { $aa }
# Create the remainder of the password from any of the allowed characters
$password += Get-Random -Count ($PasswordLength - $MinimumSpecialCharacters) -InputObject ($punc + $numbers + $letters) `
|% -Begin { $aa = $null } `
-Process { $aa += ([char]$_) } `
-End { $aa }
# Return the password but shuffled to ensure that the password doesn't always start with
# special characters
return -join ($password.ToCharArray() | Sort-Object { Get-Random })
}
Get-Password -PasswordLength 14 -MinimumSpecialCharacters 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment