Skip to content

Instantly share code, notes, and snippets.

@brettmillerb
Last active March 11, 2021 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettmillerb/a94f044de1eddf298468c46ad03e6bdd to your computer and use it in GitHub Desktop.
Save brettmillerb/a94f044de1eddf298468c46ad03e6bdd to your computer and use it in GitHub Desktop.
Get-RandomString
function Get-RandomString {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateRange(12,[int]::MaxValue)]
[int]
$StringLength,
[int]
$SpecialCharacters = 4
)
process {
foreach ($randomString in $StringLength) {
$chars = ((65..90) + (97..122) | ForEach-Object { $_ -as [char]})
$specialChars = ((33..46) | ForEach-Object { $_ -as [char]})
$outputString = $chars | Get-Random -Count ($StringLength - $SpecialCharacters)
$outputString += $SpecialChars | Get-Random -Count $SpecialCharacters
-join ($outputString | Get-Random -Count $StringLength)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment