Last active
August 23, 2024 17:43
-
-
Save gregjhogan/2350eb60d02aa759c9d269c3fc6265b1 to your computer and use it in GitHub Desktop.
Generate random string in powershell
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
#lowercase letters/numbers only | |
-join ((48..57) + (97..122) | Get-Random -Count 32 | % {[char]$_}) | |
# all characters | |
-join ((33..126) | Get-Random -Count 32 | % {[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 ) })
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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]$_}})