How to Pick Random Items from an Array in PowerShell?

Sometimes, you might need to pick a random item from a PowerShell array. In this PowerShell tutorial, I will explain to you how to pick random items from an array in PowerShell. Let us explore how to use PowerShell to get random items from an array.

To pick random items from an array in PowerShell, you can use the Get-Random cmdlet. For example, if $array is your array, you can retrieve a single random item with $randomItem = Get-Random -InputObject $array, or multiple random items by specifying the -Count parameter, like $randomItems = Get-Random -InputObject $array -Count 3 for three random items

Get Random Items from an Array in PowerShell

An array is a data structure that can hold multiple values, typically of the same type, in an ordered list. In PowerShell, you create an array by assigning multiple values to a variable, separated by commas.

$array = 'New York', 'Chicago', 'Phoenix', 'Philadelphia', 'San Diego', 'New York'

Now, let us see, how to pick random items from an array in PowerShell.

1. Using the Get-Random Cmdlet

The simplest way to select a random item from an array in PowerShell is by using the Get-Random cmdlet. This cmdlet is designed to return a random value. When used with an array, it will select a random item from the array.

Here is a basic usage of the Get-Random cmdlet to get random items from a PowerShell array.

Here’s how you can use Get-Random to select a single random item from an array:

$array = 'New York', 'Chicago', 'Phoenix', 'Philadelphia', 'San Diego', 'New York'
$randomItem = $array | Get-Random
Write-Output $randomItem

This will output a single random item from the array $array.

I executed the above PowerShell script using VS code and you can see the output in the screenshot below:

get random element from array powershell

2. Get Multiple Random Items from a PowerShell array

If you want to select multiple random items from an array in PowerShell, you can use the -Count parameter of the Get-Random cmdlet.

$array = 'New York', 'Chicago', 'Phoenix', 'Philadelphia', 'San Diego', 'New York'
$randomItems = Get-Random -InputObject $array -Count 3
$randomItems

This command will select three random items from $array. You can see the screenshot for your reference.

Pick Random Items from an Array in PowerShell

3. Randomizing the Entire PowerShell Array

Sometimes, you may want to shuffle the entire array rather than picking individual items. You can do this by passing the entire array to Get-Random with the -Count parameter equal to the length of the array.

$array = 'New York', 'Chicago', 'Phoenix', 'Philadelphia', 'San Diego', 'New York'
$shuffledArray = Get-Random -InputObject $array -Count $array.Length
$shuffledArray

This will give you a new array with the items in a random order.

4. Custom Random Selection Function in PowerShell

You might want to write a custom function for more control over the random selection process. This function could include logic to prevent duplicates when selecting multiple items or to handle weighted random selections.

Here’s a simple example of a custom function that selects a random item from an array in PowerShell:

function Get-RandomItem {
    param(
        [Parameter(Mandatory=$true)]
        [array]$Items
    )

    $index = Get-Random -Minimum 0 -Maximum $Items.Length
    return $Items[$index]
}

$randomItem = Get-RandomItem -Items $array
Write-Output $randomItem

This function uses Get-Random to generate a random index, and then returns the item at that index in the array.

Conclusion

Selecting random items from an array in PowerShell is easy using the Get-Random cmdlet. PowerShell provides the functionality you need to get the job done easily, whether you’re just picking a single item or multiple items, or shuffling the entire array.

Remember to use the -Count parameter when you need to select multiple random items and consider writing a custom function if you need more complex random selection behavior.

In this PowerShell tutorial, I have explained how to get random elements from an array in PowerShell.

You may also like: