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