Fun with PowerShell Get-Random

Introduction

Many people know about PowerShell’s Get-Random cmdlet. Using it you can generate a random value. For example:

Get-Random

Will output a value such as:

64031951

But did you know there’s other features of Get-Random?

Numeric Ranges

With Get-Random you can lock down the return result to a specific range. You can specify a minimum value as well as a maximum.

Get-Random -Minimum 100 -Maximum 200

122

You can run it numerous times and the return value will always be between 100 and 200.

You don’t have to use both the minimum and maximum values, you can just use one or the other.

While be default it returns an unsigned 32 bit integer, you can also use it to return a random floating point value.

Get-Random -Minimum 10.5 -Maximum 20.9

11.9004625960255

Get-Random and Arrays

You can also use Get-Random with Arrays. Let’s say you have an array of integer values. You can pipe it through Get-Random and it will pick one value from the array for you.

$array = 1, 3, 5, 7, 9
$array | Get-Random

7

The cool thing about this is it works with any array. Let’s say you have an array of states, and want to pick a state at random.

$array = (
  'Alabama', 'Alaska', 'Arizona',
  'Arkansas', 'California', 'Colorado',
  'Connecticut', 'Delaware', 'Florida',
  'Georgia', 'Hawaii', 'Idaho',
  'Illinois', 'Indiana', 'Iowa'
)
$array | Get-Random

Florida

This also works with arrays that hold a collection of objects. Let’s get a collection of file objects from our C:\Temp folder and let it pick one for us.

$files = Get-ChildItem -Path "C:\Temp"
$files | Get-Random

    Directory: C:\Temp

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           1/14/2021  2:31 PM                curl-7.74.0

Getting More Than One Random Item

Get-Random also has a -Count parameter which you can use to get multiple values back. Let’s use our state example, and let it return five states.

$array = (
  'Alabama', 'Alaska', 'Arizona',
  'Arkansas', 'California', 'Colorado',
  'Connecticut', 'Delaware', 'Florida',
  'Georgia', 'Hawaii', 'Idaho',
  'Illinois', 'Indiana', 'Iowa'
)
$array | Get-Random -Count 5

Idaho
Alaska
California
Delaware
Colorado

This works with any type of array, or just random integers.

Get-Random -Count 3

1981714362
1664558041
1474367023

SetSeed

Get-Random has one more parameter, -SetSeed. You can pass in a value, and when you do Get-Random will always return the same result for that seed.

Get-Random -SetSeed 100

1881691673

No matter how many times you run Get-Random, as long as you use the same seed value, it will always return a result of 1881691673. This also works with other types of arrays such as the array of states we used earlier.

$array = (
  'Alabama', 'Alaska', 'Arizona',
  'Arkansas', 'California', 'Colorado',
  'Connecticut', 'Delaware', 'Florida',
  'Georgia', 'Hawaii', 'Idaho',
  'Illinois', 'Indiana', 'Iowa'
)
$array | Get-Random -SetSeed 33

Illinois

Again, run this as much as you want and with the same SetSeed value it will always return the same state, in this case Illinois.

So why use SetSeed? One main use it testing. You want to create a set of reusable tests, for example using Pester, and want to validate you always get the same value back from your array.

Conclusion

In this post we saw how to use Get-Random for a variety of purposes. Hopefully you’ll find it as useful as I do!

I have many PowerShell related courses on Pluralsight. All of my courses are linked on my About Me page.

If you don’t have a Pluralsight subscription, just go to https://pluralsight.com. At the top is a Try For Free button you can use to get a free 10 day subscription to Pluralsight, with which you can watch my courses, or any other course on the site. Once there just search for my name, Robert Cain, to see all the courses I have.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s