Skip to content

Instantly share code, notes, and snippets.

@felickz
Forked from onlyann/generatePassword.ps1
Created April 16, 2021 17:34
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 felickz/15ca8028b08f25751f76edc86730ea3c to your computer and use it in GitHub Desktop.
Save felickz/15ca8028b08f25751f76edc86730ea3c to your computer and use it in GitHub Desktop.
$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