Weekend Scripter: Create Entries for Birthdays from Outlook Contacts List

Weekend Scripter: Create Entries for Birthdays from Outlook Contacts List

  • Comments 2
  • Likes

Summary: Use Windows PowerShell to access your Outlook Contacts list and create calendar entries for birthdays.

Honorary Scripting Guy, Sean Kearney here. And today is my birthday!

You would think that being an IT person with Microsoft Outlook and other resources in front of me, it would be very easy to track birthdays. You would THINK that.

But I’m…let’s say…absent minded.

Now the interesting thing is that in many cases, I will track a lot of the birthdays in my Contacts when I create them within Outlook. For example, here is one for a fake person called Mr. Smith:

Image of calendar entry

As you can see I have his birthday stored away with his Contact details so that I should be able to at least remember he has some type of birthday. But of course, we have a snag…that is that I’m lazy.

If I look at my calendar, we’ll see a small glitch. On this most important day of days for Dr. Smith—while he’s floating up in his big spinning space saucer—as I glance at my calendar, I notice something missing. His birthday is not entered.

Image of calendar

“Odd…” I think to myself because Outlook 2013 will automatically populate the calendar entry for birthdays. But then I realize that this contact was created a long time ago, and so his birthday was not self-populated by Outlook.

There are a few ways to resolve this.

  1. Type in the calendar entry directly.
  2. Edit the birthday manually and hope that it works to repopulate it.
  3. Let Windows PowerShell find the birthday in the Contacts list and build the calendar entry for me.

Because I think there is more than one birthday that might be missing, and I happen to like playing with Windows PowerShell on a Saturday, well I’ll go the Windows PowerShell route.

So our first challenge: Connect to Outlook with Windows PowerShell:

$Outlook=NEW-OBJECT –comobject Outlook.Application

Well, that was easy. So next we want to get our list of contacts in Outlook. We do this by accessing the Contacts list, which is a default folder type within Outlook.

To access special folders we access the GetSpecialFolder method in the Outlook ComObject. Contacts are DefaultFolder type #10.

$Outlook.session.GetDefaultFolder(10)

But this is just the folder as an object. We need to access the individual items. This is done by literally tacking on items as a property:

$Outlook.session.GetDefaultFolder(10).items

Displaying it on the screen, of course, is only cosmetic. We need to store this away so we can access the properties directly:

$Contacts=$Outlook.session.GetDefaultFolder(10).items

Now if we execute Get-Member against this object, we’ll see that there are many useful properties. We want to see if we can read the birthday date and maybe something with the name details.

$Contacts | GET-MEMBER –membertype Property *Birth*,*Name*

In the following image, we can see lots of Name properties, and the birthday date is clearly accessible. For our purposes, I would like to access only the Display Name and the Birthday. I can use these details to build a birthday in the calendar:

Image of command output

$Contacts | Select-object Fullname,Birthday

But wait, some people might not have a birthday entered! I check the contact information for Doctor Smith’s handler, Will Robinson. For some reason, I do not have his birthday recorded, so I can see what a blank birthday looks like:

Image of command output

So blank dates for the birthday will be represented somewhere far, far, far in the future— January 1 in the year 4501! About the same time Dr. Smith will learn to behave.

So let’s put this into Windows PowerShell and store the information away:

$BirthdayInfo=$Contacts | Select-Object FullName,Birthday | Where { $_.Birthday –ne ([datetime]”1/1/4501”) }

Good! Now that we have the needed information, we’ll want to populate the entries in the calendar. Let’s create a new calendar item. This is done by accessing the CreateItem() method in Outlook and specifying Item Type # 1 for appointments:

$NewEntry=$Outlook.CreateItem(1)

We need to supply some information for our calendar entry, specifically the Date it is to occur and some Subject details:

$NewEntry.Subject=”John Q Smith Birthday”

$NewEntry.Start=[datetime]”5/3/1921”

I would like this to appear as a recurring appointment, so we’ll need to access the Recurrence property of the calendar item. This is actually done in a weird way. We need to get the recurrence pattern first:

$Recur=$NewEntry.GetRecurrencePattern()

When we have the recurrence pattern as an object, we need to supply it some information. First we have to supply some special values. It’s an all-day event, which is 1,440 minutes. Our interval is once every 12 months (unless you know of somebody who has a birthday more often than that):

$Recur.Duration=1440

$Recur.Interval=12

Now we specify that it’s a yearly occurrence and that it never ends:

$Recur.RecurrenceType=5

$Recur.Noenddate=$TRUE

When we have this information set, we need only save the calendar item:

$NewEntry.save()

Now we pull all of this together into a simple loop to have the computer do the work for us:

# Connect to Outlook

$Outlook=NEW-OBJECT –comobject Outlook.Application

$Contacts=$Outlook.session.GetDefaultFolder(10).items

# Get only Contacts with a valid Birthday

$BirthdayInfo=$Contacts | Select-Object FullName,Birthday | Where { $_.Birthday –ne ([datetime]”1/1/4501”) }

# Step through the list of contacts and create Birthdays in the Calendar

Foreach ($Entry in $BirthdayInfo)

{

# Create New Calendar Item

$NewEntry=$Outlook.CreateItem(1)

# Populate Calendar Details

$NewEntry.Subject=$Entry.Fullname+”’s Birthday”

$NewEntry.Start=$Entry.Birthday

# Access Recurrence pattern

$Recur=$NewEntry.GetRecurrencePattern()

# Setup Recurrence Patterns

$Recur.Duration=1440

$Recur.Interval=12

$Recur.RecurrenceType=5

$Recur.Noenddate=$TRUE

# Save the Calendar entry

$NewEntry.save()

}

And now we have a way to go through all of my contacts and NOT forget all of their birthdays! So much easier than type…type type…type type type…type…*GrOooooAAAAaaaannn!*

Now back to a little birthday cake and a toast to any of you who are using Windows PowerShell. The best present of all for an IT pro!

By the way if you’re wondering about a birthday tune, download the Windows PowerShell Popcorn Tune from the TechNet Gallery, and replace the $MusicString contents with these lines:

# Our 'Sheet Music' - 5 characters define each note and beat.

# First three are the note, the last pair are the beat

$MusicString=@"

BGN80BGN80CAN40BGN40CCN40CBN20BGN80BGN80CAN40BGN40CDN40CCN20BGN80BGN80CGN20CEN20CCN20CBN20CAN10CFN80CFN80CEN40CCN40CDN40CCN10

"@

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send an email to the Scripting Guys at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, remember that the Power of Shell is in You.

Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Tnx!!! :)