PowerShell Genie

Get-Random

Use Get-Random to return a random number, or pick random items from a collection.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Get-Random

Syntax

1PS> Get-Random [[-Maximum] <object>] [-Minimum <Object>] [-SetSeed <Int32>] [-InputObject <Object[]>] [-Count <Int32>] [-Shuffle] [<CommonParameters>]
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>]
  1. Stage 1Get-Random-Maximum -Minimum -SetSeed -InputObject -Count -Shuffle

    Try it

    Windows PowerShell

    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.

    Interactive simulation · fictional data

    Examples

    Get-Random -Minimum 1

    1PS> Get-Random -Minimum 1 -Maximum 7
    Show reference output

    Sample output · fictional data, shaped like the real thing

    4

    Select Name, Status

    1PS> Get-Service | Get-Random -Count 3 | Select-Object 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

    1PS> Get-Random -InputObject @('alpha' , 'beta' , 'gamma') -Count 2
    Show reference output

    Sample output · fictional data, shaped like the real thing

    37

    Parameters

    Parameter definitions:

    • -Maximum
    • -Minimum
    • -SetSeed
    • -InputObject
    • -Count
    • -Shuffle

    Get-Random on Microsoft Learn

    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.

    Get-Random reference on Microsoft Learn

    Recap

    Get-Random Use Get-Random to return a random number, or pick random items from a collection.

    Next: Get-Date
    Genie guideGet-Random

    Use Get-Random to return a random number, or pick random items from a collection. The console below is pre-loaded — run it, then change a value and run it again.