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.


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


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).


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,, and Anderson (Portuguese).

Related Post

MethodNotAllowed when using Backup-AzureKeyVaultSe... I’ve been working with Azure Automation in the last few days, and during the creation of the code I was using PowerShell ISE to backup and restore Azu...
How to connect to Microsoft Azure using PowerShell In Today’s tutorial, we are going over the process to connect your Windows 10/Windows Server 2016 in Microsoft Azure to manage your tenant. In the ...
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 ...
PowerShell and logon Scripts In this Tutorial we are going over the process to create a logon script using PowerShell and that will work for all systems running at least Windows S...