Secret Santa with Powershell

Sorting out Secret Santa with Powershell

So its nearly that time of year again where we are all thinking about christmas. With being back at the office (albeit split between home and office) and missing out on christmas cheer last year, I thought it was time to get the secret santa ball rolling.

As we are split into teams and have offices in 2 locations, pulling names out of a hat is a little bit more difficult and not quite covid safe. So with my interest in Powershell, I thought why not send everyone an email with their secret santa recipient.

So with the help of Owen C’s script on Github I was able to put together a cut down version of his script which was perfect for my use.

This is my final secret santa script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$Santas = Import-Csv CSVFILE

[Array]$santalist = ($Santas | get-random -count $Santas.Count)

For($i = 0; $i -lt $santalist.Count; $i++) {
    if ($i -eq $santalist.Count-1) {
        # Puts the last person in the randomised list with the first person.
        $Recipient = $santalist[0]        
    }
    else {
        # Picks the next person in the list.
        $Recipient = $santalist[$i + 1]
    }

    $SecretSanta = New-Object -TypeName PSCustomObject -Property @{
        SantaName = $santalist[$i].Name
        SantaEmail = $santalist[$i].Email
        RecipName = $Recipient.Name
        RecipEmail = $Recipient.Email
    }

    $emailParams = @{
        Subject = "You are Secret Santa for..."
        Body = "$($SecretSanta.RecipName)."
        To = "$($SecretSanta.SantaEmail)"
        From       = "[email protected]"
        SMTPServer = ""
        Port = 25
        BodyAsHTML = $true
        ## UseSSL = $true
    }

    Send-MailMessage @emailParams
}

The CSV file contains all the users taking part in the secret santa and consists of 2 columns, Name and Email. e.g:

Name Email
Name1 [email protected]

The script will loop through each user and send them an email containing the name of who they are buying for in the secret santa.

You will need to change the $emailparams to your details.

Built with Hugo
Theme Stack designed by Jimmy