# 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)") | |
This comment has been minimized.
This comment has been minimized.
teeTime |
This comment has been minimized.
This comment has been minimized.
great! |
This comment has been minimized.
This comment has been minimized.
A great solution for me! Thanks |
This comment has been minimized.
This comment has been minimized.
Great that you all find the code useful! |
This comment has been minimized.
This comment has been minimized.
Thanks. Pretty simple and useful. But sometimes it misses the numbers,,, |
This comment has been minimized.
This comment has been minimized.
Yes, the selection is randomized and the string might not contain numbers. Likewise it could be a string with only numbers, which is more likely if you generate a short string. If you need a string with more even distribution of word character and digits there are likely better ways to generate then using my function. |
This comment has been minimized.
This comment has been minimized.
Thanks Marc. I found this topic useful too: |
This comment has been minimized.
This comment has been minimized.
Any idea why there is a limit of 62 chars? |
This comment has been minimized.
This comment has been minimized.
The function creates string in the intervals 0-9A-Z a-z and then sample randomize character from that string. Why Get-Random stops sampling after 62 chars I don't know, but I guess it is dependent on the string length. The function was created to generate fairly randomized short string to be included in some test files back in history. I don't think I have used it to generate any strings longer then 10 chars so I didn't know about the 62 chars limit. I don't write any things for PowerShell at this moment and don't have easy access to a Windows environment so I'm unable to dig in and find out why the 62 chars limit but If anybody knows feel free to write a comment here. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Here is my final function with the reference to the source, which I modified a bit .EXAMPLE
} # End of Function Generate-Password |
This comment has been minimized.
This comment has been minimized.
Great! Thanks for improving this tread! |
This comment has been minimized.
This comment has been minimized.
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! |
This comment has been minimized.
Very helpful, thanks!