Created
August 5, 2020 19:08
-
-
Save ToshY/8ce83657db0956c11a4f8e1a7b85fc1c to your computer and use it in GitHub Desktop.
Random string with PS
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
[CmdletBinding()] | |
<# | |
.SYNOPSIS | |
Create a random string of alphanumeric characters | |
.DESCRIPTION | |
This function creates a random string of alphanumeric characters. | |
Alphanumeric characters are put into an array, joined, randomly picked and shuffled once more. | |
.PARAMETER length | |
The length of the random string to be returned. | |
Default [int] 20. | |
.PARAMETER n | |
This parameters makes it possible to have alphanumeric characters occur more than once in the string. | |
Setting this parameter manually to 1 will return a random string without duplicates. | |
Default [int] [math]::Floor([math]::sqrt($length)). | |
.EXAMPLE | |
.\randomString.ps1 | |
.EXAMPLE | |
.\randomString.ps1 -length 25 | |
.EXAMPLE | |
.\randomString.ps1 -length 25 -n 1 | |
.NOTES | |
NAME: randomString | |
AUTHOR: ToishY | |
GIT: https://github.com/ToishY/randomString | |
CREATED: 05/01/2019 | |
LASTEDIT: 05/01/2019 | |
VERSION: 1.0 | |
#> | |
Param( | |
[Parameter(Mandatory=$false)] | |
[int]$length = 20, | |
[Parameter(Mandatory=$false)] | |
[int]$n = [math]::Floor([math]::sqrt($length)) | |
) | |
function getRandomString{ | |
Param( | |
[Parameter(Mandatory=$true)] | |
[int]$len, | |
[Parameter(Mandatory=$true)] | |
[int]$m | |
) | |
return ( ( ( -join ( ( (48..57)*$m ) + ( (65..90)*$m ) + ( (97..122)*$m ) | Get-Random -Count $len | % { [char]$_} ) ) -split '' | Sort-Object {Get-Random}) -join '' ) | |
} | |
# getRandomString | |
getRandomString -len $length -m $n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment