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 $randomItemThis 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:

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
$randomItemsThis command will select three random items from $array. You can see the screenshot for your reference.

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
$shuffledArrayThis will give you a new array with the items in a random order. You can see the exact output in the screenshot below:

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:
$array = 'New York', 'Chicago', 'Phoenix', 'Philadelphia', 'San Diego', 'New York'
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 $randomItemThis function uses Get-Random to generate a random index, and return the item at that index in the array. You can check the output in the screenshot below:

Conclusion
Selecting random items from an array in PowerShell is easy using the Get-Random cmdlet. By using the Get-Random cmdlet in PowerShell, you can pick a single item or multiple items or shuffle 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:
- How to Get Array Length in PowerShell?
- Get the First Element of an Array in PowerShell
- Split an Array into Smaller Arrays in PowerShell
- Remove Items from an Array in PowerShell
- How to Get Unique Values from an Array in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.