Skip to content

Using Get-Random in PowerShell

Black and White PDQ logo
Kris Powell|Updated January 26, 2021
Generic blog header
Generic blog header

People here at PDQ.com know how much I love Futurama, and Zapp Brannigan quotes are some of my favorites.

Since I can never decide on my favorite quote, it only seemed natural to throw together a quick PowerShell function to get a random Zapp quote any time that I wanted.

I’ve had a few people ask me about it, so I figured that I’d throw a blog together.(drum roll please) Enter PowerShell!

In the game of chess, you can never let your adversary see your pieces

Zapp Brannigan, Futurama

Get-Random

In order to randomly choose from a set of items, we are going to make use of the Get-Random cmdlet.Get-Random will pick a random number or a random object from a collection of objects.By default, Get-Random will pick a random number between 0 and the maximum 32-bit integer value (2,147,483,647).

Get-Random
PowerShell Get Random

You can also define a minimum and maximum value to randomly choose from. The maximum value, however, will never be chosen so you’ll need to pick a value that’s 1 higher than the range you’re expecting. For example, if you want to select a number between 1-10, you would do the following:

Get-Random -Minimum 1 -Maximum 11
Get Random  Minimum 1  Maximum 11

It also works for lists of objects. Gone are the days of having to worry about selecting your meal on an airplane. I’ll pipe over my choices (more info about the pipeline) and have PowerShell choose my meal!

"Chicken", "Beef" | Get-Random
"Chicken", "Beef" | Get Random

Can’t decide on a delicious bourbon to drink? Let’s use an array! You can pipe the array in or you can specify it as the input object.

$MyFancyBourbon = @("Bulleit Bourbon - Barrel Strength", "Pappy Van Winkle (any)", "Maker's 46", "Angel's Envy", "Woodford Reserve", "Four Roses") $MyFancyBourbon | Get-Random Get-Random -InputObject $MyFancyBourbon
Random bourbon and scotch

Now onto the fun stuff!

Zapp Brannigan quotes

Pick your favorite quotes and put them in an array. Then, pipe that array to Get-Random and voila! You’re done!

Function Get-ZappQuote {    $QuoteList = @(        "Stop exploding, you cowards.",        "Uh-huh.Uh-huh. That's whatever you were talking about for ya.",        "Kif! Where's the little umbrella? That's what makes it a scotch on the rocks.",        "If we hit that bullseye, the rest of the dominoes should fall like a house of cards. Checkmate.",        "I am the man with no name, Zapp Brannigan at your service!",        "Don't be such a chicken, Kif. Teenagers smoke, and they seem pretty on-the-ball.",        "Fire all weapons and set a transmission frequency for my victory yodel.",        "I got your distress call and came here as soon as I wanted to.",        "We'll just set a new course for that empty region over there, near that blackish, holeish thing.",        "In the game of Chess, you must never let your adversary see your pieces.",        "I've always thought of myself as a father figure to some of my more pathetic men.",        "You win again, Gravity!",        "Has my fame preceded me? Or was I too quick for it?",        "Care for some champaggin?",        "I have made it with a woman. Inform the men!",        "If there's an alien out there I can't kill, I haven't met them and killed them yet",        "As you all know, the key to victory is the element of surprise.  ...surprise!",        "When I'm in command, every mission is a suicide mission.",        "Come on girdle... hold!",        "Fly the white flag of war."    )    $QuoteList | Get-Random } Get-ZappQuote
Zapp Brannigan quote generator

This is not an exhaustive list of awesome Zapp quotes, but it’s enough to get anyone started! Now you can enjoy one of your Zapp quotes anytime you use the function, Get-ZappQuote. Amazing!

Creating a PDQ Inventory tool with your PowerShell Function

I’ve even taken this a step further and threw it together as a tool in PDQ Inventory. I dot source my Get-ZappQuote script and then run the function. Here’s how I create my tool (complete with my default tool description in the output).

  • Open PDQ Inventory and click on Tools in the tree.

  • On the Tools Menu tab, click New > Tool.

  • In the Tool window, in addition to adding your script, ensure the following is selected: PowerShell, Leave Shell Open, and System Tool (no selected computer needed).

Futurama Inspirational Zapp

Here is how I run my tool from within PDQ Inventory. It doesn’t require that a machine is selected, so I can run the tool from any screen. Simply click Tools > Tool name and away it runs.

PDQ Inventory 13 Enterprise Mode

Wrapping up

The practical applications of Get-Random are pretty much endless. Anytime you find yourself unable to make a choice, consider throwing PowerShell at the problem! You, my friend have just created a PowerShell Magic 8-ball!

Yes or No

Black and White PDQ logo
Kris Powell

Kris was an employee at PDQ.

Related articles