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

How to rename an AWS EC2 instance during provision... In the Cloud era the VM/instance name is not that important, especially when we have a fleet of servers doing the same work, and that is key for scena...
Removing Azure Active Directory users using Azure ... Cloud Shell is one of the new features in Azure Portal, and it is accessible from virtually any browser and even from Azure App. In this Tutorial, we ...
Using PowerShell as default option in Windows 10 m... By default in Windows 10 (well, on previous versions as well) when we either use right-click on the lower-left corner or Windows + X , we will have a ...
Listing the drive letters, size and free space usi... Using PowerShell, the administrator is able to retrieve a lot of information from the operating system. In Today’s Tutorial, we are going the process ...