I run a giveaway campaign recently.
After the campaign end, I will have to select a winner.
In the beginning, I was thinking that open a book on the random page to select a winner. However, it is not professional, and it cannot be recorded.
So, I decide to write a PowerShell script for it. I am surprised that it is so simple!
Here is the code
# 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
First, you will need a CSV file that contains the email list. The header is called “Email.” You can call it whatever you want as long as it matches the name in line 8.
Get-Random is the cmdlet to select an object randomly. You can also have the multiple winners by changing the count value.
Here is the result.