Get a random number, or select objects randomly from a collection.
Syntax Get-Random [-InputObject] Object [-Count int] [-SetSeed int] [CommonParameters] Get-Random [[-Maximum] Object] [-Minimum Object] [-SetSeed int] [CommonParameters] key -Count int Determines how many objects are returned. If the value of Count exceeds the number of objects in the collection, Get-Random returns all of the objects in random order. The default is 1. -InputObjectObject A collection of objects. Get-Random gets randomly selected objects in random order from the collection. Enter the objects, a variable that contains the objects, or a command or expression that gets the objects. A collection of objects may also be piped to Get-Random. -Maximum Object A maximum value for the random number. Get-Random will return a value that is less than the maximum (not equal). Enter a 32-bit integer or a double-precision floating-point number, or an object that can be converted, such as a numeric string ("100"). The value of -Maximum must be greater than (not equal to) the value of -Minimum. If the value of -Maximum or -Minimum is a floating-point number, Get-Random will return a randomly selected floating-point number. If the value of Minimum is a double (a floating-point number), the default value of Maximum is Double.MaxValue. Otherwise, the default value is Int32.MaxValue (2,147,483,647) -Minimum Object A minimum value for the random number. Enter a 32-bit integer or a double-precision floating-point number, or an object that can be converted, such as a numeric string ("100"). The default value is 0 (zero). The value of Minimum must be less than (not equal to) the value of Maximum. If the value of -Maximum or -Minimum is a floating-point number, Get-Random will return a randomly selected floating-point number. -SetSeed int A seed value for the random number generator. This seed value is used for the current command and for all subsequent Get-Random commands in the current session until SetSeed is used again The seed cannot be reset back to its default, clock-based value. -SetSeed is not required. By default, Get-Random uses the system clock to generate a seed value. Using SetSeed will result in non-random behavior, it is typically used to reproduce behavior, such as when debugging or analyzing a script.
Get-Random gets a randomly selected number.
If a collection of objects is submitted to Get-Random, then one or more randomly selected objects from the collection will be returned.
An alternative approach is to create a random object:
$objRand = new-object random
$num = $objRand.next(1,500)
Get a random integer between 0 (zero) and Int32.MaxValue:
PS C:> get-random
Get a random integer between 0 (zero) and 99:
PS C:> get-random -maximum 100
Get a random integer between -100 and 99:
PS C:> get-random -minimum -100 -maximum 100
Get four randomly selected numbers in random order from an array:
PS C:> get-random -input 1, 2, 3, 5, 8, 13 -count 4
Get a randomly selected sample of 25 files from the C: drive of the local computer:
PS C:> $files = dir -path c:\* -recurse
PS C:> $sample = $files | get-random -count 25
“Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin” - John von Neumann
Related PowerShell Cmdlets:
Get-Unique - Return the unique items from a sorted list.
Random Numbers in CMD or .js