Gets a random number, or selects objects randomly from a collection.
# Get a random integer between 0 and 99
Get-Random -Maximum 100
# Get a random integer between -100 and 99
Get-Random -Minimum -100 -Maximum 100
# -65
# Get a random floating-point number
Get-Random -Minimum 10.7 -Maximum 20.93
# 18.08467273887
# Get a random integer from an array
1, 2, 3, 5, 8, 13 | Get-Random
# 8
# Get several random integers from an array
1, 2, 3, 5, 8, 13 | Get-Random -Count 3
# 3
# 1
# 13
# Randomize an entire collection
1, 2, 3, 5, 8, 13 | Get-Random -Shuffle
# 2
# 3
# 5
# 1
# 8
# 13
# Get a random non-numeric value
"red", "yellow", "blue" | Get-Random
# yellow
# Get random files
$Files = Get-ChildItem -Path C:\* -Recurse
$Sample = $Files | Get-Random -Count 50
# Roll fair dice
# This example rolls a fair die 1200 times and counts the
# outcomes. The first command, ForEach-Object repeats the call to Get-Random
# from the piped in numbers (1-6). The results are grouped by their value with
# Group-Object and formatted as a table with Select-Object.
1..1200 | ForEach-Object {
1..6 | Get-Random
} | Group-Object | Select-Object Name, Count
# Name Count
# ---- -----
# 1 206
# 2 199
# 3 196
# 4 226
# 5 185
# 6 188
# Use the Count parameter
# You can now use the Count parameter without piping objects to Get-Random.
# The following example gets three random numbers less than 10.
Get-Random -Count 3 -Maximum 10
# 9
# 0
# 8
# Use the InputObject parameter with an empty string or $null
# In this example, the InputObject parameter specifies an array that contains an empty string ('') and $null.
Get-Random -InputObject @('a','',$null)
# Get-Random returns either a, empty string, or $null.
# The empty sting displays as a blank line and $null returns to a PowerShell prompt.