Randomizing collections: one-liners
I found a cool way HERE to shuffle/reorder array elements without using the random class.
Generally you provide Sort-Object an object property to sort on, only now the property is in the form of a Globally Globally Unique Identifier (GUID), which is a random generated 128-bit integer.
PS > 1..5 | sort-object { [guid]::newGuid() }
2
5
3
1
4
To select one or more random objects from the collection, pipe the above to Select-Object and use the -First (or -Last) parameter:
PS > 1..5 | sort-object { [guid]::newGuid() } | select-object -first 3
4
1
3
These techniques are also available on collections you use everyday with PowerShell, not just numbers. For instance, selecting 4 random lines from a text file (contain 10 numbered lines), you get the idea :-)
PS > Get-Content lines.txt | sort-object { [guid]::newGuid() } | select-object -last 4
Line 3
Line 4
Line 2
Line 5