Created
April 5, 2017 13:05
-
-
Save marcgeld/4891bbb6e72d7fdb577920a6420c1dfb to your computer and use it in GitHub Desktop.
Powershell: Generate a random Alphanumeric string
This file contains 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
# 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)") | |
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 ?
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
This is a great solution to my issue of generating a password without special characters!
The only concern I had was that characters could not be re-used, which drastically reduces the randomness of the strings it makes. So I worked out a solution that hopefully can help some other people frustrated by the GeneratePassword forced random characters.
The 62 Character limit is because the Get-Random function will not get the same character twice. When it gets, it essentially pops that item out of the array and can no longer choose it.
Here is a way around it which seems to work quite well.
This may or may not use every character in the array, as the array is refreshed after each letter is added.
Btw, first time posting on git. Woot!