-
-
Save gregjhogan/2350eb60d02aa759c9d269c3fc6265b1 to your computer and use it in GitHub Desktop.
#lowercase letters/numbers only | |
-join ((48..57) + (97..122) | Get-Random -Count 32 | % {[char]$_}) | |
# all characters | |
-join ((33..126) | Get-Random -Count 32 | % {[char]$_}) |
Nice to have
But Get-Random will get each item in the list only once. If the value of Count exceeds the number of objects in the collection, Get-Random returns all of the objects in random order, meaning a weaker security for a password.
It would be better to use :
-join ((48..57) * n | Get-Random -Count 32 | % {[char]$_})
n being whatever you want, 10, 100, 1000...-join ((48..57) | Get-Random -Count 32 | % {[char]$}) 2758460139 -join ((48..57) *120 | Get-Random -Count 32 | % {[char]$})
67297844472509842658108029803065
another way to get around it is to generate the number of random characters that you want setting the minimum and maximum number within get-random. Its longer, and does the same thing, just a different way.
-join ((1..100) | %{get-random -minimum 33 -maximum 126 | %{[char]$_}})
Thanks a lot!
My string for random alphanumeric string based on solutions above:
-join (((48..57)+(65..90)+(97..122)) * 80 |Get-Random -Count 12 |%{[char]$_})
-join (((48..57)+(65..90)+(97..122)) * 80 |Get-Random -Count 12 |%{[char]$_})
Bam, this is exactly what I am looking for!! Thank you both.
Keep in mind that the -Maximium is exclusive unlike the array.
so to include 126 do
-join ((1..100) | %{get-random -minimum 33 -maximum 127 | %{[char]$_}})
Using ForEach
with a nested call to Get-Random
on the input ranges is another way to avoid repeats:
-join (1..20 | ForEach {[char]((97..122) + (48..57) | Get-Random)})
if hex digits are good enough you might consider this
-join ((1..8) | % { "{0:X}" -f ( Get-Random -min 0 -max 256 ) })
Nice to have
But Get-Random will get each item in the list only once. If the value of Count exceeds the number of objects in the collection, Get-Random returns all of the objects in random order, meaning a weaker security for a password.
It would be better to use :
-join ((48..57) * n | Get-Random -Count 32 | % {[char]$_})
n being whatever you want, 10, 100, 1000...
-join ((48..57) | Get-Random -Count 32 | % {[char]$})
2758460139
-join ((48..57) *120 | Get-Random -Count 32 | % {[char]$})
67297844472509842658108029803065