Skip to content

Instantly share code, notes, and snippets.

@TakamiChie
Created January 13, 2022 06:08
Show Gist options
  • Save TakamiChie/0fc00113ed8d81b547c645cef7a206f9 to your computer and use it in GitHub Desktop.
Save TakamiChie/0fc00113ed8d81b547c645cef7a206f9 to your computer and use it in GitHub Desktop.
Simple Password Generator
# PassGen -> Output 8-character password
# PassGen 10 -> Output a 10-character password.
# PassGen 10 $false -> Do not include symbols.
# PassGen 10 $false $false -> Do not include symbols and numbers.
# PassGen 10 $true $false $false -> Symbols only (setting all to false will of course result in an error)
# The maximum length of the password that can be output is 93 characters.
# Useful Usage).
# PassGen | clip -> Copy the generated password directly to the clipboard
# Write a function in WindowsPowerShell\Microsoft.PowerShell_profile.ps1(PowerShell folder for PowerShell 7.x) -> You can use the PassGen command at any time.
function PassGen($length=8, $symbol=$true, $number=$true, $alpha=$true) {
if(($length -le 0) -or ($length -ge 94)){
Write-Error -Message "The number of characters specified is too large or too small. This command can only generate passwords between 0-93 characters." -ErrorAction Stop
}
$c = @()
if($alpha){
$c = $c + ([char[]]([char]65..[char]90)) + ([char[]]([char]97..[char]122))
}
if($number){
$c = $c + ([char[]]([char]48..[char]57))
}
if($symbol){
$c = $c + ([char[]]([char]33..[char]47)) + ([char[]]([char]58..[char]64)) + ([char[]]([char]91..[char]96)) + ([char[]]([char]123..[char]126))
}
if($c.count -ge 1){
($c | sort {Get-Random})[0..$length] -join ''
}else{
Write-Error -Message "Nothing to display!" -ErrorAction Stop
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment