The Get-Random Cmdlet

The information in this article was written against the second Community Technology Preview (CTP2) of Windows PowerShell 2.0. This information is subject to change in future releases of Windows PowerShell 2.0.

Randomize Your Results with the Get-Random Cmdlet

No doubt when it came time for the 2008 Winter Scripting Games many of you thought, “Gee, I’d love to enter the Scripting Games, except for one thing: the solutions to the Scripting Games often require you to use random numbers, and Windows PowerShell doesn’t have a cmdlet that makes it a snap to generate random numbers. If PowerShell had a cmdlet for generating random numbers, why, I’d enter the Scripting Games in a heartbeat!” Well, we’ve got good news for those of you who’ve been waiting for a random number generator to be added to Windows PowerShell: just wait until you see Get-Random, a new cmdlet added to the Windows PowerShell 2.0 of PowerShell.

Looks like we’ll be seeing you at the 2009 Winter Scripting Games, eh?

Of course, Get-Random does have uses beyond just helping you rack up points in the Winter Scripting Games. Before we discuss those uses, however, let’s take a moment to explain how Get-Random actually works. In its simplest form, you can call Get-Random without any additional parameters; in turn, Get-Random will generate a random integer between 1 and 2,147,483,647. In other words:

PS C:\Scripts> Get-Random

And there you have it.

Being able to generate a random number between 1 and 2,147,483,647 is useful for the Scripting Guys; after all, that helps them keep track of the money in their checking account. More often than not, however, you’ll want to place slightly-stricter limits on the random numbers that get generated; for example, you might want to choose a random number between 1 and 100. That’s fine; Get-Random lets you do just that, provided you include the –minimum and –maximum parameters:

PS C:\Scripts> Get-Random -minimum 1 -maximum 101

And yes, -maximum must always be set to 1 more than the high number in the range. That means that, to select a random number between 1 and 100, we must set –maximum to 101; if we set –maximum to 100 then we’d actually select a random number between 1 and 99.

By contrast, -minimum is always set to the actual low end of the range. What if we wanted to select a random number between 200 and 299? Then we’d use this command:

PS C:\Scripts> Get-Random -minimum 200 -maximum 300

And so on.

But here’s the really cool thing about Get-Random: it can do more than just generate random numbers. For example, you can use Get-Random to randomly select from a list of names:

($a = "Dasher","Dancer","Prancer","Vixen","Comet","Cupid","Donder","Blitzen" ) | Get-Random

All we’ve done here is create an array named $a and then pipe that array to Get-Random; in turn, Get-Random will randomly select one of the items in the array:


Incidentally, we could perform this same task by using the –input parameter rather than by creating an array:

Get-Random -input "Dasher","Dancer","Prancer","Vixen","Comet","Cupid","Donder","Blitzen"

Oh, and here’s a cool thing. What if you wanted to randomly select three reindeer? That’s easy; just add the –count parameter, like so:

($a = "Dasher","Dancer","Prancer","Vixen","Comet","Cupid","Donder","Blitzen" ) | Get-Random -count 3

That’s going to give us output similar to this:


You say that you don’t have an array of reindeer names, instead you have all the reindeer names in a text file like this one:


That’s fine; just use the Get-Content cmdlet to read in the contents of the text file, then pipe that information to Get-Random:

Get-Content C:\Scripts\Test.txt | Get-Random

In this case Get-Random will randomly select a line from the text file:


As far as we know, Get-Random handles pretty much any data you throw at it. For example, suppose, for testing purposes, you need to randomly select three files from the folder C:\Scripts. This command should do the trick:

Get-ChildItem C:\Scripts | Get-Random -count 3

In other words:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/30/2007  11:33 AM        977 test1.vbs
-a---        10/24/2007   2:42 PM      32124 new_cmdlets.txt
-a---         7/24/2007   9:54 AM        273 identify_winrm.vbs

Etc., etc.

Related Links