Skip to content

Instantly share code, notes, and snippets.

@R41D3NN
Created June 13, 2018 18:41
Show Gist options
  • Save R41D3NN/e4a5b169971baf8a3481d29218b65f2d to your computer and use it in GitHub Desktop.
Save R41D3NN/e4a5b169971baf8a3481d29218b65f2d to your computer and use it in GitHub Desktop.
Generate a random string.
Function Get-RandomString {
Param(
[Int] $Length = 10,
[Bool] $IncludeNumerical = $True,
[Bool] $IncludeLowerCase = $True,
[Bool] $IncludeUpperCase = $True
)
$Local:NumericalSet = (48..57)
$Local:LowerCaseSet = (65..90)
$Local:UpperCaseSet = (97..122)
$Local:Set = $null
If ($IncludeNumerical) {
$Local:Set += $Local:NumericalSet
}
If ($IncludeLowerCase) {
$Local:Set += $Local:LowerCaseSet
}
If ($IncludeUpperCase) {
$Local:Set += $Local:UpperCaseSet
}
If ($Local:Set.Length -Eq 0) {
Throw "Set cannot be empty! Cannot generate a string from nothing..."
}
Return (-join ((1..$Length) | % { $Local:Set | Get-Random } | % {[char]$_}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment