Created
July 25, 2016 11:52
-
-
Save dazfuller/9dbb98d2fc163d147068629f35e9a5ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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