Get-Random
Use Get-Random to return a random number, or pick random items from a collection.
Documentation: Microsoft Learn — Get-Random
Syntax
See the command anatomy
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Get-Random [[-Maximum] <object>] [-Minimum <Object>] [-SetSeed <Int32>] [-InputObject <Object[]>] [-Count <Int32>] [-Shuffle] [<CommonParameters>]- Stage 1Get-Random
-Maximum -Minimum -SetSeed -InputObject -Count -Shuffle
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Follow the objects
Run the command to inspect each stage.
Examples
Get-Random -Minimum 1
Show reference output
Sample output · fictional data, shaped like the real thing
4
Select Name, Status
Show reference output
Sample output · fictional data, shaped like the real thing
Name Status ---- ------ BITS Running Spooler Stopped WinRM Running
Get-Random -Count 2
Show reference output
Sample output · fictional data, shaped like the real thing
37
Parameters
Parameter definitions:
-Maximum-Minimum-SetSeed-InputObject-Count-Shuffle
Practical use and troubleshooting
Practical scenario: Use Get-Random when you need to return a random number, or pick random items from a collection. Review syntax and sample output, then try an example in the browser lab. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.
Common error and fix: A mistaken expectation of a secure secret is a design error. Use an appropriate cryptographic random facility for secrets rather than treating an ordinary teaching sample as a security guarantee. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.
See also: Get-Date. Get-Random chooses sample values; Get-Date reports time. Random selection and a timestamp solve different problems, and neither alone is a suitable secret-management strategy. Read the linked command before substituting it; similar names do not imply identical inputs or output.
Recap
Get-Random Use Get-Random to return a random number, or pick random items from a collection.
Next: Get-Date