Author Posts

January 3, 2018 at 9:02 pm

How can i for example choose to get 10 difrent random characters from a long string/word?
String example: "asdfadfnglkaslkngalkfnk2342419!1342@sadfadsfad?123412341adfafa"

January 3, 2018 at 9:08 pm

$string = "asdfadfnglkaslkngalkfnk2342419!1342@sadfadsfad?123412341adfafa"

for ($i = 0; $i -lt 10; $i++ ) {
    $newString += $string[(Get-Random -Minimum 0 -Maximum $string.Length)]


January 3, 2018 at 9:14 pm

Awesome, thanks!

January 3, 2018 at 9:21 pm

Should probably have explained what I did, sorry about that.

A string, such as $string in the above code, is simply an array of characters. To navigate an array, you choose the index that you want to move to. Such as the following:

$string = "asdfadfnglkaslkngalkfnk2342419!1342@sadfadsfad?123412341adfafa"

where 0 and 12 are indexes in the array.
So what I did was used the Get-Random cmdlet and limited the results to between 0 and the length of the string. Get-Random returns and int value and that is used to navigate the character array.

January 3, 2018 at 9:23 pm

Ah, thanks for explaining. Now i learned something new 🙂

January 4, 2018 at 4:49 pm

Different Method

$string = "asdfadfnglkaslkngalkfnk2342419!1342@sadfadsfad?123412341adfafa"
$array = $string -split '' | Where-Object {$_ -ne ''}
Get-Random -InputObject $array -Count 10