Created
December 29, 2020 14:05
-
-
Save onlyann/00d9bb09d4b1338ffc88a213509a6caf to your computer and use it in GitHub Desktop.
This file contains hidden or 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