PowerShell: How to Generate a Random Number?
Are you looking for a way to generate random numbers in PowerShell? There could be countless reasons why you may need to generate a random number in PowerShell, including testing data, Randomization, Simulation, etc. In this blog post, we will discuss the steps needed to generate random numbers with PowerShell, along with some PowerShell script examples. Let’s explore the different ways you can generate random numbers in PowerShell!
To generate a random number in PowerShell, you can use the Get-Random
cmdlet.
PowerShell comes with its native function for generating random numbers using the Get-Random cmdlet, which adds powerful functionality to your automation scripts without having to write complex algorithms or search online for third-party modules.
# Generate a random number
$RandomNumber = Get-Random
This will generate a random number between 32-bit integer 0 and 2147483647. If you want to generate a random floating-point number, just provide the -Maximum
parameter in decimal. E.g.
Get-Random -Maximum 10.0
Generate a random number within range in PowerShell
If you would like to generate a random number within a specific range, you can use the -Minimum
and -Maximum
parameters to specify a range for the random number. Here’s an example of how you can use it to generate a random integer between a specific range:
$RandomNumber = Get-Random -Minimum 1 -Maximum 10
This will assign a random integer between 1 and 9 (Not 10! Always set one number higher in maximum) to the $RandomNumber
variable. Need a six digit random number? No issues!
Get-Random –Minimum 000000 -Maximum 999999
Get Random from Array in PowerShell
You can also use the -InputObject
parameter to specify an array from which the random number should be chosen. For example:
$RandomNumber = Get-Random -InputObject 1,2,3,4,5,6,7,8,9,10
You can also pass in an array of integers as arguments to Get-Random; this will return one of the integers from the array at random. For example:
# create an array with values 1 through 5
$Values = @(1, 2 ,3 ,4 ,5)
# Get a random value from $values at random
Get-Random -InputObject $Values
This will generate a random element from the given array. Similarly, if you want to select an item from an array of strings randomly:
#Create an array of strings
$Array = @('Apple','Banana','Orange','Pear')
# Get an array of strings
$Random = Get-Random -InputObject $Array
#Get-Random cmdlet accepts the input object from Pipeline as well
$Array | Get-Random
To generate multiple numbers at once, use the -Count
parameter:
Get-Random -InputObject 1, 2, 3, 4, 5 -Count 2
Here is another way to generate multiple random numbers:
1..5 | ForEach-Object { Get-Random -Minimum 1 -Maximum 10 } | Sort -Unique
Conclusion
As we have seen above, these random number generating techniques in PowerShell can be used for various purposes, such as selecting a random item from arrays or performing other tasks where randomized results are needed. With just a few lines of code, you can easily create randomly generated numbers, both integers and floats.