Skip to content

Instantly share code, notes, and snippets.

@massihm
Forked from marcgeld/psRandomAlphaNumeric.ps1
Last active May 21, 2021 12:58
Show Gist options
  • Save massihm/b23732185c2e4c0b0285eab0d04687aa to your computer and use it in GitHub Desktop.
Save massihm/b23732185c2e4c0b0285eab0d04687aa to your computer and use it in GitHub Desktop.
Powershell: Generate a random Alphanumeric string
# Generate a random Alphanumeric string
Function Get-RandomString {
[CmdletBinding()]
Param (
[int] $length = 8,
[switch]$alphabet,
[switch]$number,
[switch]$symbol
)
Begin{
[int[]] $charset =@();
if($alphabet){
$charset += (0x41 .. 0x5A) + (0x61 .. 0x7A)
# ("A" .. "Z" ) + ("a" .. "z" )
}
if($number){
$charset += (0x30 .. 0x39)
# ("0" .. "9" )
}
if($symbol){
$charset += (0x20 .. 0x2F) + (0x3A .. 0x40) + (0x5B .. 0x60) + (0x7B .. 0x7E)
# (" " .. "/" ) + (":" .. "@" ) + ("[" .. "``") + ("{" .. "~" )
}
if($charset.Length -eq 0){
$charset += (0x30 .. 0x7E)
}
}
Process{
Write-Output ( -join ((0..$length) | %{[char]($charset | Get-Random)}) )
}
}
Export-ModuleMember -Function Get-RandomString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment