Skip to content

Instantly share code, notes, and snippets.

@marcgeld
Created April 5, 2017 13:05
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save marcgeld/4891bbb6e72d7fdb577920a6420c1dfb to your computer and use it in GitHub Desktop.
Save marcgeld/4891bbb6e72d7fdb577920a6420c1dfb to your computer and use it in GitHub Desktop.
Powershell: Generate a random Alphanumeric string
# Generate a random Alphanumeric string
Function Get-RandomAlphanumericString {
[CmdletBinding()]
Param (
[int] $length = 8
)
Begin{
}
Process{
Write-Output ( -join ((0x30..0x39) + ( 0x41..0x5A) + ( 0x61..0x7A) | Get-Random -Count $length | % {[char]$_}) )
}
}
# Write-Host ("Alfa Beta " | Tee-Object -Variable txt) ($txt.length)
Write-Host "A: "(Get-RandomAlphanumericString | Tee-Object -variable teeTime ) ("len=$($teeTime.length)")
Write-Host "B: "(Get-RandomAlphanumericString -length 22 | Tee-Object -variable teeTime ) ("len=$($teeTime.length)")
@me-suzy
Copy link

me-suzy commented Apr 10, 2021

ok, in powershell, does anyone know how to make a shuffle words, on several txt files, search and replace like this https://onlinerandomtools.com/shuffle-words ?

@BeMor81
Copy link

BeMor81 commented Jul 8, 2022

As others have said, the maximum length is 62 and also the characters are never reused so greatly reduces the complexity of the string. The below works around these issues

Write-Output ( -join ($(for($i=0; $i -lt $length; $i++) { ((0x30..0x39) + ( 0x41..0x5A) + ( 0x61..0x7A) | Get-Random | % {[char]$_}) })) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment