Generating random numbers in PowerShell

Share this:

When creating PowerShell scripts, very often we need to generate random numbers to use in several scenarios. In Today’s post, we are going a couple of examples how to use it.

Solution


The easiest way to use random is typing random and a random number will be generated, however, we have some flexibility to get random numbers based on our requirements.

We can use the Get-Random cmdlet and provide the maximum and minimum values to define the range to return a number within that defined boundaries, as depicted in the image below.

Get-Random -Maximum 50 -Minimum 30

image

A good example to use random is when using Start-Sleep and we want to add some random delays. In the example below, we create a random number between 5 and 15, and we add 6 and start a delay (in that example of 16 seconds).

image

Written by Anderson Patricio

Anderson Patricio

Anderson Patricio is a Canadian MVP in Cloud and Datacenter Management, and Office Server and Services, besides the Microsoft Award he also holds a Solutions Master (MCSM) in Exchange and several other certifications. Anderson has been contributing to the Microsoft Community with articles, tutorials, blog posts, twitter, forums and book reviews. He is a regular contributor here at ITPROCentral.com, MSExchange.org, Techgenix.com and Anderson Patricio.org (Portuguese).

Related Post

Managing Hyper-V Server remotely through PowerShel... Working with PowerShell can be very common for daily tasks and Hyper-V Server management. However, as there is more than one server to be managed, som...
Using Azure Resource Manager in PowerShell How to connect to Microsoft Azure PowerShell and use Azure Resource Manager deployment mode. Solution Microsoft Azure is a dynamic environment...
Creating a PowerShell Function to display error me... When writing your code using PowerShell, and based on the complexity of the script we need to create output to troubleshoot/debug the code.We can alwa...
How to install Microsoft Azure PowerShell In this Tutorial we are going over the process to install Microsoft Azure PowerShell and we can use the same procedure to update several other compone...