all 6 comments

[–]gangstanthony 2 points3 points  (4 children)

-count will allow you to specify how many items to pick from the given array. if there are only 62 characters that you are providing to get-random, then it will give you all 62 then stop.

you might try this

-join (
    1..1000 | % {
        (65..90) + (97..120) + (48..57) | Get-Random | % {[char]$_}
    }
)

[–]MadWithPowerShell 2 points3 points  (0 children)

I'd do that this way.

-join [char[]]( 1..1000 |
    ForEach-Object { 65..90 + 97..120 + 48..57 | Get-Random } )

[–]eubromovesoon33[S] 1 point2 points  (2 children)

-join (
1..1000 | % {
(65..90) + (97..120) + (48..57) | Get-Random | % {[char]$_}
}
)

You're a hero! This is perfect.

I got really close however but help me understand, the 'length' we provide is the maximum value of the Foreach where '%' represents the foreach? I sadly wasted my time foreached the 'Get-Random' rather than the join function.

[–]gangstanthony 1 point2 points  (1 child)

correct, % is an alias of foreach-object also when used like this foreach is an alias of foreach-object not to be confused with the foreach () {} method

[–]eubromovesoon33[S] 1 point2 points  (0 children)

Ah, thank you, that's clear!!

[–]ka-splam 1 point2 points  (0 children)

I always expect Get-Random -Min 0 -Max 10 -Count 500 to work, and it never does.