Skip to content

Instantly share code, notes, and snippets.

@beercanx
Created April 21, 2026 15:45
Show Gist options
  • Select an option

  • Save beercanx/e256a05b2e7859e490e82355d96d8a4b to your computer and use it in GitHub Desktop.

Select an option

Save beercanx/e256a05b2e7859e490e82355d96d8a4b to your computer and use it in GitHub Desktop.
Generate Password in PowerShell

Generating a password in PowerShell

A quick, offline, alphanumeric with symbols password generator.

##
# Password Generator: min 1 lower | upper | number | symbol
##
function GeneratePassword([int] $length = 24) {
  
  # Character sets
  $lowerCase = [char[]]([char]'a'..[char]'z')
  $upperCase = [char[]]([char]'A'..[char]'Z')
  $number = [char[]]([char]'0'..[char]'9')
  $symbol = '_-+#!£$%^&*@,.?~:;'.ToCharArray()
  
  # Initial password
  $pass = ""
  
  # Minimum of one of each type
  $pass += ($lowerCase | Get-Random -Count 1) -join ''
  $pass += ($upperCase | Get-Random -Count 1) -join ''
  $pass += ($number    | Get-Random -Count 1) -join ''
  $pass += ($symbol    | Get-Random -Count 1) -join ''
  
  # Fill in the rest of the character selection
  $pass += ($($lowerCase;$upperCase;$number;$symbol) | Get-Random -Count ($length - $pass.Length)) -join ''
  
  # Randomly shuffle the selected characters
  $pass = ($pass -split '' | Sort-Object {Get-Random}) -join ''
  
  # Print it out
  Write-Host $pass
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment