Generate a Secret Santa List with PowerShell

It’s supposed to be the most wonderful time of the year and while you might buy multiple Christmas gifts for everyone in your immediate family, often times buying for everyone in your extended family or for all of your co-workers is cost prohibitive.

I originally started out with a simple idea to create a PowerShell script to take a list of names and generate a second random list of names based off of the first one while making sure the corresponding name on the second list doesn’t match the first one. Sounds simple enough, right?

Everything seemed well and fine until I figured out there was a problem with my logic because if the last entry in both lists are the same, there’s no way to prevent a duplicate other than not performing a match at all which means someone would be left out.

It took running the code a number of times for the problem to occur. As you can see in the previous example, Alan Brewer doesn’t have a match because the only one left in the second list was himself.

I decided to take a different approach while at the same time checking to see if the last person in the two lists matched and just regenerate the second list if they did.

The problem with my second approach is that it didn’t prevent a person from being matched to themselves in the middle of the list.

Clearly this was going to be a little more difficult than I initially thought. If I’m going to put more effort into this, I’ll just create a function for it.

I attempted to use ValidateScript to make sure there were an even number of items provided, but it appears that you’re unable to retrieve the count or length property in the param block.

I nested a for loop inside the do/while loop to check each entry and if it matches itself, the break statement causes it to immediately exit the loop, regenerate the second list, and start over.

The function generates two lists with the same names that are matched randomly while making sure no one is matched to themselves.

If less than two names are provided or if an odd number of names is provided, an error is generated.

While duplicates don’t seem to occur that often, they do occur. The verbose parameter can be used to see the duplicates.

You could query Active Directory similarly to what I’ve done in the examples shown in this blog article and add their email address. Then the Send-MailMessage cmdlet could be used to automatically email each of the users with the name of the person they’re buying a gift for.

The other possibility that I thought about is simply generating a random offset less than the number of names in the list and offsetting the matching list by that number, but it wouldn’t purely random like the examples shown in this blog article. I’d love to hear your thoughts and know if there’s a simpler way to accomplish this task?

µ

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: