PowerShell 1 min read
Select Random Objects from a List by PowerShell
Michael Wu ·
I needed a way to select random winners from a giveaway campaign list rather than using unprofessional methods. Here’s a clean PowerShell approach.
PowerShell Script
# Random select winner(s) from a CSV list
# Import File path
$importPath = "C:\yourCSVfile.csv"
# Number of winners
$count = 1
$list = Import-Csv -Path $importPath | select -ExpandProperty Email
# Select random object
$winner = Get-Random -InputObject $list -Count $count
$date = Get-Date
Write-Host "The Winner(s): $winner. Selected on $date." -ForegroundColor Green
Key Points
- The CSV file needs a header column named “Email” (or adjust line 8 to match your column name)
Get-Randomis the core cmdlet for random selection- Adjust
$countto select multiple winners - The timestamp provides documentation of when the selection was made