To generate a random number in PowerShell, use the Get-Random cmdlet. For a simple random number between 0 and 2,147,483,647, simply type Get-Random and press Enter. To specify a range, use the -Minimum and -Maximum parameters, such as Get-Random -Minimum 1 -Maximum 11 to get a number between 1 and 10.

The Get-Random cmdlet is a built-in utility used for everything from simple dice rolls and lottery simulations to selecting random files from a folder or random users from a list. It is highly versatile because it can handle both individual integers and entire collections of objects.

What You'll Need

  • A computer running Windows, macOS, or Linux with PowerShell installed.
  • Access to the PowerShell terminal (PowerShell 5.1 or PowerShell Core/7+).

Step-by-Step

  1. Open the PowerShell Terminal: Click the Start menu, type "PowerShell," and select the application. Depending on your needs, you can use the standard Windows PowerShell or the newer PowerShell 7.
  2. Generate a Basic Random Number: Type Get-Random and press Enter. This will return a non-negative random integer.
  3. Set a Specific Range: To define the boundaries of your number, use the Minimum and Maximum parameters.
    Example: Get-Random -Minimum 1 -Maximum 101
    Note: The Maximum value is exclusive, meaning the cmdlet will generate numbers up to, but not including, the maximum. In the example above, the result will be between 1 and 100.
  4. Generate a Random Floating-Point Number: If you need a decimal (double) between 0.0 and 1.0, use the -Maximum parameter without a minimum, or use a specific range and divide the result. However, the standard way to get a random double is to use the [Random]::new().NextDouble() .NET method.
  5. Pick a Random Item from a List: You can pass an array of items to Get-Random to pick one at random.
    Example: 'Red', 'Blue', 'Green', 'Yellow' | Get-Random
  6. Pick Multiple Random Items: To select more than one unique item from a list, use the -Count parameter.
    Example: 1..100 | Get-Random -Count 5
    This creates a range of numbers from 1 to 100 and randomly selects 5 unique values from that set.

Common Mistakes

  • Forgetting the Exclusive Maximum: The most common error is expecting the Maximum value to be included. If you want a number between 1 and 6 (like a die), you must set the maximum to 7.
  • Incorrect Parameter Order: While PowerShell is flexible, ensure you are using the parameter names -Minimum and -Maximum clearly to avoid confusion, especially when scripting.
  • Using Get-Random for Cryptography: Get-Random is a pseudo-random number generator. It is perfect for scripts and basic automation, but it is not cryptographically secure. Do not use it to generate passwords or encryption keys.
  • Confusing Count with Range: The -Count parameter does not generate a number of a certain size; it determines how many items are picked from an input collection.

Tips

  • Using a Pipeline: You can pipe any list of objects—such as a list of files from Get-ChildItem—into Get-Random to pick a random file for processing.
  • Storing the Result: If you need to use the random number later in your script, assign it to a variable: $myNumber = Get-Random -Minimum 1 -Maximum 11.
  • Generating a Range Quickly: Use the range operator .. to create a list quickly. For example, 1..10 creates an array containing all integers from 1 to 10.
  • Formatting Output: If you are generating numbers for a report, you can pipe the result into Out-String or use string interpolation to include the number in a sentence.

Troubleshooting

If the command does not work as expected, try these steps:

  • Check for Typos: Ensure there is a space between Get-Random and the parameters. PowerShell is not case-sensitive, but the spelling must be exact.
  • Verify PowerShell Version: While Get-Random is available in almost all versions, some advanced piping behaviors differ between Windows PowerShell 5.1 and PowerShell 7. Run $PSVersionTable to check your version.
  • Check Input Types: If you are piping a list to Get-Random and getting an error, ensure the list is actually an array or a collection. If the input is a single string, it may treat each character as an item.
  • Restart the Console: If the terminal is hanging or not responding to commands, close the window and reopen it to clear the session state.

Frequently Asked Questions

Is the maximum number included in the result?

No, the -Maximum value is exclusive. If you set the maximum to 10, the highest possible number you will receive is 9.

Can I generate a random negative number?

Yes. By setting the -Minimum parameter to a negative value (e.g., -Minimum -100 -Maximum 0), you can generate negative random integers.

How do I generate a random number with decimals?

Get-Random primarily handles integers. To get a decimal, you can use the .NET framework via [Random]::new().NextDouble(), which returns a value between 0.0 and 1.0.

Can I use Get-Random to pick a random file from a folder?

Yes. Use the command Get-ChildItem | Get-Random. This retrieves all items in the current directory and pipes them into the random selector.

Does Get-Random produce the same sequence every time?

No, it uses a seed based on the system clock, so every time you run the command in a new session, the results will be different.