Generate random number using Powershell


randomI was faced with an opportunity that I had to create a set of random numbers really quick.
The question was to create a list of numbers starting from 1 to 9999 and supply a list with this range where each number was only used once and was placed in the list randomly.

Because Powershell is my first choice as scripting language I searched for a function in Powershell that handles random numbers.

I eventually created the script below:

1..9999 | Get-Random -Count 9999 | Out-File -Append -FilePath "C:\Temp\random.txt"

This code takes an array of the value 1 to 9999 and outputs the random number to a text file.

Leave a Reply

Your email address will not be published.