Skip to content

Instantly share code, notes, and snippets.

@onlyann
Created December 29, 2020 14:05
Show Gist options
  • Save onlyann/00d9bb09d4b1338ffc88a213509a6caf to your computer and use it in GitHub Desktop.
Save onlyann/00d9bb09d4b1338ffc88a213509a6caf 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