-
-
Save felickz/15ca8028b08f25751f76edc86730ea3c 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
$symbols = '!@#$%^&*'.ToCharArray() | |
$characterList = 'a'..'z' + 'A'..'Z' + '0'..'9' + $symbols | |
function GeneratePassword { | |
param( | |
[Parameter(Mandatory = $false)] | |
[ValidateRange(12, 256)] | |
[int] | |
$length = 14 | |
) | |
do { | |
$password = "" | |
for ($i = 0; $i -lt $length; $i++) { | |
$randomIndex = [System.Security.Cryptography.RandomNumberGenerator]::GetInt32(0, $characterList.Length) | |
$password += $characterList[$randomIndex] | |
} | |
[int]$hasLowerChar = $password -cmatch '[a-z]' | |
[int]$hasUpperChar = $password -cmatch '[A-Z]' | |
[int]$hasDigit = $password -match '[0-9]' | |
[int]$hasSymbol = $password.IndexOfAny($symbols) -ne -1 | |
} | |
until (($hasLowerChar + $hasUpperChar + $hasDigit + $hasSymbol) -ge 3) | |
$password | ConvertTo-SecureString -AsPlainText | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment