Generate random numbers in PowerShell with Get-Random
Have you ever wanted to generate random numbers for passwords, test data…. and wondered how you could easily generate them?
Well I was confronted by the same situation on one of our servers where I needed some random numbers for inputting wait times to test an application but didn’t have my usual tool of choice ( MS Excel ) available to me.
I remembered the Get-Random command from memory but used the Get-Help command to allow me to use the correct syntax.
Executing Get-Random will produce a random number.
This was quite a good start but then I needed only 4 digit figures and I then had to use the minimum and maximum parameters.
Get-Random -Maximum 9999 -Minimum 1000
The above command solved that problem for me and I think it reflects how easily readable the PowerShell language is.
The last scenario I had was producing random numbers for some temporary passwords. I looked at the help for Get-Command and noticed the inputobject and count parameters which I thought would fulfil my need.
Get-Random -InputObject (10000..99999) -Count 3
The above command shows the range of values which I gave and the number of random numbers I needed in the Count parameter.
Hopefully the above will help you with your random number needs. Please add your feedback or other solutions in the comments below.
Originally published at https://parvtheitgeek.com on April 16, 2015.