This PowerShell script randomly selects a winner from a pool of participants based on ticket entries listed in an Excel spreadsheet. It outputs the winner's name and draw timestamp to the console and logs the results to an Excel file.
Before running this script, ensure you have:
-
PowerShell Module:
Install theImportExcel
module if it's not already installed:Install-Module -Name ImportExcel
-
Excel Data File:
The script expects the input data to be in a file namedraffle.xlsx
which needs to be in the same dictonary like theRaffle.ps1
. Ensure this Excel file exists and has the following structure:Name Tickets Alice 5 Bob 3 Charlie 2 - Column 1:
Name
– Participant's name. - Column 2:
Tickets
– The number of tickets each participant has purchased.
- Column 1:
-
File Path:
The script expects this file to exist at the same path asRaffle.ps1
- Imports participants and their ticket counts from the Excel file.
- Each participant's name is added to a virtual "ticket pool" based on the number of tickets they purchased.
Example:
If Alice purchased 5 tickets, her name will be added 5 times to the ticket pool.
- Randomly selects one name from the "ticket pool."
- The draw result (date and winner) is logged to a worksheet named
Results
within the Excel file. - If the worksheet already exists, new results will be appended.
- If the Excel file does not exist, it will be created.
- The winner's name and the draw timestamp are displayed in German date format:
Winner: Alice
Winner drawn at 10.12.2024 09:22
-
Setup Prerequisites:
EnsureImportExcel
is installed and the input file is ready with proper data. -
Run the Script:
Run the script using PowerShell:.\raffle.ps1
-
Check Results:
- Results will be saved to the
Results
worksheet in the input Excel file. - The console will show:
Winner: [Winner's Name] Winner drawn at [Date and Time in German format]
- Results will be saved to the
The draw result is saved to the Results
worksheet in the Excel file, with the following columns:
DateTime | Winner |
---|---|
10.12.2024 09:22 | Alice |
If you encounter issues writing to the Excel file:
- Ensure you have write permissions to the file's directory.
- Close Excel if it is open to avoid locking the file.
If the script fails, ensure you install the ImportExcel
module:
Install-Module -Name ImportExcel
This script is licensed under MIT License.
Feel free to fork, modify, or share.