-
-
Save massihm/b23732185c2e4c0b0285eab0d04687aa to your computer and use it in GitHub Desktop.
Powershell: Generate a random Alphanumeric string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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