Created
June 13, 2018 18:41
-
-
Save R41D3NN/e4a5b169971baf8a3481d29218b65f2d to your computer and use it in GitHub Desktop.
Generate a random string.
This file contains hidden or 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
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