Using Get-Random in PowerShell

You can roll the dice, select a random name with your eyes closed, etc., or you can use Get-Random 🙂 In this blog post, I will show you what the Cmdlet does and how you can use it.

What is Get-Random?

“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.

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

Source: Get-Random (Microsoft.PowerShell.Utility) – PowerShell | Microsoft Learn

How does it work?

Parameters

You can use the Get-Random Cmdlet with Parameters to let it return a random thing that you want from a list, a number range, etc.

Count

You can use this to return the number of random objects to return. The default value is 1, but you can use it to return more if needed. If the value is higher than the number of objects, all objects will be returned because it can’t match your specified value.

InputObject

You can use this to specify the collection of objects that Get-Random randomly selects items from. This can be a variable, $Total, or a command or expression like Get-Process.

Minimum

You can use this, together with -Maximum if needed, to specify the minimum value for a random number. (Integer, double-precision floating-point number, or any object that can be converted to an integer or double, a numeric string like “100, for example. Default value is 0.

Maximum

Like you would expect, you can use this, together with -Minimum if needed, to specify the maximum value for a random number. It must be greater than, not equal to, the value of -Minimum.

SetSeed

This can be used to specify a seed value for the random number generator. When you use SetSeed, the Cmdlet generates pseudorandom numbers, which aren’t cryptographically secure. PowerShell 7.4 includes Get-SecureRandom, which ensures cryptographically secure randomness.

Note: Setting the seed results in non-random behavior. It should only be used when trying to reproduce behavior, such as when debugging or analyzing a script that includes Get-Random commands.

Note: This seed value is used for the current command and for all subsequent commands. Get-Random Commands In The current session lasts until you use SetSeed again or close the session. You can’t reset the seed to its default value.

Shuffle

This will shuffle the entire collection in a randomized order, like a deck of cards 😉

Notes

Get-Random doesn’t always return the same data type as the input value. The following table shows the output type for each numeric input type.

Input TypeOutput Type
SByteDouble
ByteDouble
Int16Double
UInt16Double
Int32Int32
UInt32Double
Int64Int64
UInt64Double
DoubleDouble
SingleDouble

Beginning in Windows PowerShell 3.0, Get-Random supports 64-bit integers. In Windows PowerShell 2.0, all values are cast to System.Int32.

Beginning in PowerShell 7, the InputObject parameter in the RandomListItemParameterSet parameter set accepts arrays that contain an empty string or $null. In earlier PowerShell versions, only the Maximum parameter in the RandomNumberParameterSet parameter set accepted an empty string or $null.

Is it really random?

Yes! And no… It’s based on “System.Random” which uses a time-based seed. Because of this, you can get the same result back when triggered in quick succession.

Examples

Dice

You can use this to simulate rolling a single die (Which has a minimum of 1 and a maximum of 6, of course), for example, in two formats:

In the first line, it took the 1-6 range from the Pipeline. In the second example, I used the -Minimum and -Maximum Parameters.

Shuffle

In this example, I use the Pipeline to specify a few numbers, and the -Shuffle Parameter selects two of them in a random order using the -Count Parameter.

Random name

If you would like to select someone to do the dishes, for example, you can also specify names instead of numbers in the Pipeline to pick someone 🙂

Random files

And you can also select a few random files from a folder, which works because Get-Random allows Commands or Expressions as InputObject. For example:

Wrapping up

And that’s where you can use Get-Random for, so that you won’t have to choose yourself 😉 Have a lovely weekend!

One thought on “Using Get-Random in PowerShell

  1. A recent issue (Fall 2025 – sorry, unsure of the month) of Popular Mechanics has a very interesting article on random numbers, and what it means to be random.

Leave a Reply to danCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.