Skip to content

Instantly share code, notes, and snippets.

@thetalljosh
Forked from onlyann/generatePassword.ps1
Last active August 5, 2024 23:27
Show Gist options
  • Select an option

  • Save thetalljosh/a569d2adcfb230b57be958f4a415302c to your computer and use it in GitHub Desktop.

Select an option

Save thetalljosh/a569d2adcfb230b57be958f4a415302c 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 = (Get-Random -Minimum 15 -Maximum 25)
)
$passwordBuilder = New-Object System.Text.StringBuilder
do {
$passwordBuilder.Clear()
for ($i = 0; $i -lt $length; $i++) {
$randomIndex = [System.Security.Cryptography.RandomNumberGenerator]::GetInt32(0, $characterList.Length)
$passwordBuilder.Append($characterList[$randomIndex]) | Out-Null
}
$password = $passwordBuilder.ToString()
$charChecks = @{
'Lowercase' = $password -cmatch '[a-z]'
'Uppercase' = $password -cmatch '[A-Z]'
'Digit' = $password -match '[0-9]'
'Symbol' = $password.IndexOfAny($symbols) -ne -1
}
}
until ($charChecks.Values.Where({$_}).Count -ge 3)
$password | ConvertTo-SecureString -AsPlainText
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment