How to Generate Random Numbers in PowerShell?

Recently, someone asked me about different methods to generate random numbers in PowerShell. I suggested different methods with examples. In this tutorial, I will show you how to generate random numbers in PowerShell with examples.

To generate random numbers in PowerShell, you can use the Get-Random cmdlet. For example, to generate a single random number, simply run Get-Random, which will output a random 32-bit integer. If you need a random number within a specific range, use Get-Random -Minimum 1 -Maximum 100 to get a random number between 1 and 99.

Generate Random Numbers in PowerShell

In PowerShell, you can use the Get-Random cmdlet to generate random numbers and select random elements from collections.

Let me show you an example of creating a single random number in PowerShell using this cmdlet.

The simplest use of the Get-Random cmdlet is to generate a single random number. By default, Get-Random generates a random 32-bit integer. This will generate a random number between 0 and Int32.MaxValue (2,147,483,647).

$randomNumber = Get-Random
Write-Output $randomNumber

By default, Get-Random generates a non-negative random integer. I executed the above PowerShell script, and you can see the exact output. It generated a random number, as shown in the screenshot below.

powershell random number

Check out PowerShell Global Variables

Generate random numbers within range in PowerShell

Now, let me show you how to generate a random number within range in PowerShell.

If you want to generate a random number within a specific range, then you need to specify the minimum and maximum values as parameters. The -Minimum parameter sets the inclusive lower bound, while the -Maximum parameter sets the exclusive upper bound.

# Generate a random number between 1 and 100
$randomNumber = Get-Random -Minimum 1 -Maximum 100
Write-Output $randomNumber

In this example, the random number will be between 1 (inclusive) and 100 (exclusive). This is useful for scenarios like generating random IDs, selecting random indexes, or any application requiring a number within a specific range.

Here is the output you can see in the screenshot below:

powershell random number within range

PowerShell: random number between 1 and 10

If you have any specific requirements, like you want to generate a random number between 1 and 10, then you can modify the above PowerShell script like the below:

# Generate a random number between 1 and 10
$randomNumber = Get-Random -Minimum 1 -Maximum 11
Write-Output $randomNumber

In this example, the random number will be between 1 (inclusive) and 11 (exclusive). This ensures that the number 10 is included in the possible outcomes. When you run the above PowerShell script, this will generate a random number between 1 and 10.

Check out PowerShell Local Variables

Generate 6 digit random number in PowerShell

To generate a random six-digit number, you can specify the range accordingly. A six-digit number ranges from 100000 to 999999, so you would set your -Minimum to 100000 and -Maximum to 1000000 (since the maximum value is exclusive).

# Generate a random six-digit number
$randomNumber = Get-Random -Minimum 100000 -Maximum 1000000
Write-Output $randomNumber

This command ensures that the generated number is always a six-digit integer. This can be particularly useful for generating random verification codes, user IDs, or any other scenario where a fixed-length number is required.

You can see the output in the screenshot below after I executed the script using VS code.

powershell random number 6 digit

Check out PowerShell Random Password Generator

Generate Multiple Random Numbers in PowerShell

Sometimes, you may need to generate multiple random numbers in PowerShell.

To generate multiple random numbers, you can use a loop or specify the -Count parameter to generate a specific number of random values.

Here is the complete PowerShell script.

# Using a loop
for ($i = 0; $i -lt 5; $i++) {
    $randomNumber = Get-Random -Minimum 1 -Maximum 100
    Write-Output $randomNumber
}

# Using -Count parameter
$randomNumbers = Get-Random -Minimum 1 -Maximum 100 -Count 5
Write-Output $randomNumbers

Once you execute the above PowerShell script, it will generate multiple random numbers. I took the screenshot below after executing the script using VS code.

Generate Multiple Random Numbers in PowerShell

Read How to Add Comments in PowerShell?

Use Get-Random with a Seed

If you need reproducible random numbers, you can use the -SetSeed parameter to specify a seed value. This is useful for testing purposes, where you might want to generate the same sequence of random numbers across multiple runs.

# Generate a random number with a seed
$randomNumber1 = Get-Random -SetSeed 42
$randomNumber2 = Get-Random -SetSeed 42

Write-Output $randomNumber1
Write-Output $randomNumber2

Both $randomNumber1 and $randomNumber2 will have the same value because the same seed is used. This can be incredibly useful for debugging and testing scripts that rely on random numbers, allowing you to reproduce the same conditions consistently. You can see the output in the screenshot below:

Use Get-Random with a Seed to Generate random number

Conclusion

In this tutorial, I explained how to generate random numbers in PowerShell using the Get-Random cmdlet. We covered generating single random numbers, specifying ranges, generating multiple random numbers, etc. I have also explained how to generate a 6-digit random number in PowerShell.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.