Skip to content

Instantly share code, notes, and snippets.

@Digiover
Last active March 6, 2026 07:20
Show Gist options
  • Select an option

  • Save Digiover/d74a76efedf1e84ddaf947b7284dfe2a to your computer and use it in GitHub Desktop.

Select an option

Save Digiover/d74a76efedf1e84ddaf947b7284dfe2a to your computer and use it in GitHub Desktop.
Easily create a random string (or secure password) using PowerShell. Add to your PS profile
# Add to your PS profile to create random strings / secure passwords
# from within your PowerShell shell.
#
# Source / Author: Daniel Kåven
# https://teams.se/powershell-script-generate-a-random-password/
function Get-RandomString {
param (
[CmdletBinding(PositionalBinding=$false)]
[Parameter(Position=0)]
[ValidateRange(8, 256)]
[int] $Length = 20,
[Parameter(Position=1)]
[validateset("AlphaNumeric", "SQLCompliant")]
[string]$Compliancy
)
$Characters = [char]65..[char]90 # A..Z
$Characters += [char]97..[char]122 # a..z
$Characters += [char]48..[char]57 # 0..9
Switch ($Compliancy){
"AlphaNumeric" {
}
"SQLCompliant" {
$Characters += [char]33 #!
$Characters += [char]35..[char]37 # #$%
}
default {
$Characters += [char]33..[char]47 # !"#&%'()*+,-./
}
}
$Password = @()
For ($i = 0; $i -lt $Length; $i++) {
$Password += $Characters[[System.Security.Cryptography.RandomNumberGenerator]::GetInt32($Characters.Count)]
}
return -join $Password
}
@Digiover
Copy link
Author

Digiover commented Mar 6, 2026

Update: Substituted Get-Random with RandomNumberGenerator::GetInt32() for a more Cryptographically Secure Pseudo-Random Number Generator (CSPRNG).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment