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

Automating the process to join a server to a domai... In this Tutorial we are going to optimize a simple task that in larger environments may take some time from the service desk/operations team which is ...
Getting the number of mailboxes per database in yo... How to create a single line PowerShell to retrieve the number of mailboxes per mailbox database in your organization. I was asked about that number in...
Script to create a VPN Site-to-Site using Azure Re... The process to create a VPN Site-to-Site in Microsoft Azure requires a series of cmdlets, and at this time the administrator cannot use Azure Preview ...
How to remove the several Organization Units that ... In some situation an entire structure of Organization Units must be deleted, and by default new OUs are automatic defined to protect them against dele...