Powershell

How to use the Get-Random (Microsoft.PowerShell.Utility)?

The PowerShell “Get-Random” cmdlet is used to select one or more objects randomly from a group of objects. Particularly, it also gets a random number from an array in PowerShell. The user can define parameters to get the minimum and the maximum number of random objects. If the parameters are not defined then, the stated cmdlet retrieves the 32-bit unsigned integers between 0 and int32.MaxValue.

The usage of the “Get-Random” cmdlet will be demonstrated in this particular post.

How to use the Get-Random (Microsoft.PowerShell.Utility)?

The stated cmdlet is specially designed to retrieve the random values including integers and strings in PowerShell.

Example 1: Utilize the Cmdlet “Get-Random” to Get/Retrieve a Random Number

Execute the below-given command to retrieve a random number in PowerShell:

Get-Random

 
Output


Example 2: Use the “Get-Random” Cmdlet to Get a Random Integer Between 10 and 50

Run the below-given code to retrieve a random integer from provided values:

Get-Random -Minimum 10 -Maximum 50

 
According to the below-provided code, first, specify the “Get-Random” cmdlet. After that, add the “-Minimum” and “-Maximum” parameters along with the stated values:


Example 3: Use the “Get-Random” Cmdlet to Get a Random Floating Number

To get the random floating number by running the below-stated command:

Get-Random -Minimum 20.5 -Maximum 57.6

 
Output


Example 4: Use the “Get-Random” Cmdlet to Get/Retrieve a Random Integer

To get the random integer number from an array execute the below command:

1, 11, 21, 31, 41, 51 | Get-Random

 
As you can see, we have retrieved the random number from the provided array:


Example 5: Use the “Get-Random” Cmdlet to Get a Random Non-Numeric Value From an Array

Use the following command to retrieve the non-numeric value randomly from an array:

"apple", "mango", "cherry" | Get-Random

 
Output


We have briefly explained the “Get-Random” cmdlet in PowerShell.

Conclusion

The “Get-Random” cmdlet in PowerShell is used to select and retrieve one or more random objects or numbers from a specific collection. In this post, we have demonstrated the “Get-Random” command with the aid of several examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.