Michael Wu

The Life of a Cloud Engineer

  • Home
  • Resume
  • Blog
  • Contact

Posted on 06.13.16

Select random objects from a list by PowerShell

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.

emaillist

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.

winner_censored

Categories:PowerShell

Recent Posts

  • Automating Resource Restriction in Azure subscriptions with Budget Alert, Automation Account, and Azure Policy
  • How to configure the Security Center via Azure Resource Manager (ARM) template
  • How to restrict Account Operators to see the password in LAPS
  • How to join a VM to a domain by PowerShell in Azure
  • How to push an existing repository from VS Code to Azure DevOps by PowerShell

Search

Categories

  • Azure
  • IIS
  • PowerShell
  • SCCM
  • VMware
  • Windows Server

Contact

Email: [email protected] | Powered By the 太初網路

Copyright © 2024 mikewu.org Disclaimer

Connect

FacebookTwitterGoogle +Linkedin