PowerShell Logo Small

Get-Random



This is the built-in help made by Microsoft for the command 'Get-Random', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Gets a random number, or selects objects randomly from a collection.

SYNTAX


Get-Random [[-Maximum] [<Object>]] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-Minimum
[<Object>]] [-SetSeed [<Int32>]] [<CommonParameters>]
Get-Random [-InputObject] <Object[]> [-Count [<Int32>]] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
[<System.String>]] [-SetSeed [<Int32>]] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Get-Random cmdlet gets a randomly selected number. If you submit a collection of objects to Get-Random, it gets one or more randomly selected objects from the collection.


Without parameters or input, a Get-Random command returns a randomly selected 32-bit unsigned integer between 0 (zero) and Int32.MaxValue (0x7FFFFFFF, 2,147,483,647).


You can use the parameters of Get-Random to specify a seed number, minimum and maximum values, and the number of objects returned from a submitted collection.



<

RELATED LINKS


Online Version: http://go.microsoft.com/fwlink/p/?linkid=293974

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>Get-Random
3951433



This command gets a random integer between 0 (zero) and Int32.MaxValue.










-------------------------- EXAMPLE 2 --------------------------

PS C:\>Get-Random -Maximum 100
47



This command gets a random integer between 0 (zero) and 99.










-------------------------- EXAMPLE 3 --------------------------

PS C:\>Get-Random -Minimum -100 -Maximum 100
56



This command gets a random integer between -100 and 99.










-------------------------- EXAMPLE 4 --------------------------

PS C:\>Get-Random -Minimum 10.7 -Maximum 20.93
18.08467273887



This command gets a random floating-point number greater than or equal to 10.7 and less than 20.92.










-------------------------- EXAMPLE 5 --------------------------

PS C:\>Get-Random -InputObject 1, 2, 3, 5, 8, 13
8



This command gets a randomly selected number from the specified array.










-------------------------- EXAMPLE 6 --------------------------

PS C:\>Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count 3
3
1
13



This command gets three randomly selected numbers in random order from the array.










-------------------------- EXAMPLE 7 --------------------------

PS C:\>Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count ([int]::MaxValue)
2
3
5
1
8
13



This command returns the entire collection in random order. The value of the Count parameter is the MaxValue static property of integers.

To return an entire collection in random order, enter any number that is greater than or equal to the number of objects in the collection.










-------------------------- EXAMPLE 8 --------------------------

PS C:\>Get-Random -InputObject "red", "yellow", "blue"
yellow



This command returns a random value from a non-numeric collection.










-------------------------- EXAMPLE 9 --------------------------

PS C:\>get-process | Get-Random

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
144 4 2080 488 36 0.48 3164 wmiprvse



This command gets a randomly selected process from the collection of processes on the computer.










-------------------------- EXAMPLE 10 --------------------------

PS C:\>Get-Content Servers.txt | Get-Random -Count (Get-Content Servers.txt).Count | foreach {Invoke-Command -ComputerName $_ -Command 'Get-Process PowerShell'}



This command runs a command on a series of remote computers in random order.










-------------------------- EXAMPLE 11 --------------------------

PS C:\>Get-Random -Maximum 100 -SetSeed 23

# Commands with the default seed are pseudorandom

PS C:\>Get-Random -Maximum 100
59
PS C:\>Get-Random -Maximum 100
65
PS C:\>Get-Random -Maximum 100
21

# Commands with the same seed are not random

PS C:\>Get-Random -Maximum 100 -SetSeed 23
74
PS C:\>Get-Random -Maximum 100 -SetSeed 23
74
PS C:\>Get-Random -Maximum 100 -SetSeed 23
74

# SetSeed results in a repeatable series

PS C:\>Get-Random -Maximum 100 -SetSeed 23
74
PS C:\>Get-Random -Maximum 100
56
PS C:\>Get-Random -Maximum 100
84
PS C:\>Get-Random -Maximum 100
46
PS C:\>Get-Random -Maximum 100 -SetSeed 23
74
PS C:\>Get-Random -Maximum 100
56
PS C:\>Get-Random -Maximum 100
84
PS C:\>Get-Random -Maximum 100
46



This example shows the effect of using the SetSeed parameter. Because SetSeed produces non-random behavior, it is typically used only to reproduce results, such as when
debugging or analyzing a script.










-------------------------- EXAMPLE 12 --------------------------

PS C:\>$files = dir -Path C:\* -Recurse
PS C:\>$sample = $files | Get-Random -Count 50



These commands get a randomly selected sample of 50 files from the C: drive of the local computer.










-------------------------- EXAMPLE 13 --------------------------

PS C:\>Get-Random 10001
7600



This command gets a random integer less than 10001. Because the Maximum parameter has position 1, you can omit the parameter name when the value is the first or only unnamed
parameter in the command.










-------------------------- EXAMPLE 14 --------------------------

PS C:\>Get-Random -Minimum ([Int64]::MinValue)3738173363251507200
PS C:\>Get-Random -Minimum ([Int32]::MaxValue)

Minimum (2147483647) cannot be greater than or equal to Maximum (2147483647).
+ CategoryInfo : InvalidArgument: (:) [Get-Random], ArgumentException
+ FullyQualifiedErrorId : MinGreaterThanOrEqualMax,Microsoft.PowerShell.Commands.GetRandomCommand



These commands attempt to get randomly generated 64-bit numbers.

The first command succeeds, but the second command fails. When the value of Minimum is a 32-bit integer, the default value of Maximum is Int32.MaxValues. The command fails
because the value of Maximum must be greater than the value of Minimum.