Monday, February 11, 2013

PowerShell: Creating & Deleting Outlook 2003-2010 Appointments

I wanted to help my finance department set up meeting reminders that most staff need on their calendars.  We couldn't find a way to create an Outlook Recurring Event since the dates varied from month to month.  For lack of an easier way to automate the decimation of these meeting reminders via Outlook, I turned to a PowerShell solution.  I found several articles and two stood out: Richard Siddaway's Delete Items blog entry and the Hey, Scripting Guys Export Calendar blog.  Using bits from both as well as other locations, I created a simple way to add and delete Calendar Appointments.


Creating Outlook Calendar Appointments


#The first two lines connect to Outlook and prepare for the new item entries

$olAppointmentItem = 1 

$o = new-object -comobject outlook.application 

#Each new calendar appointment must have the CreateItem and Save lines

#--------One complete Calendar---------------
$a = $o.CreateItem($olAppointmentItem) 
  
$a.Start = "2/14/2013 8:00 AM" 
$a.Duration = 60 
$a.Subject = "Monthly Budget Meeting" 
$a.Body = "See Agenda Items at http://www.contoso.com" 
$a.Location = "Finance Section" 
$a.ReminderMinutesBeforeStart = 15 
$a.ReminderSet = $True 
  
$result = $a.Save() 
#--------Appointment Item Entry--------------

$a = $o.CreateItem($olAppointmentItem) 
  
$a.Start = "3/11/2013 8:00 AM" 
$a.Duration = 60 
$a.Subject = "Monthly Budget Meeting" 
$a.Body = "See Agenda Items at http://www.contoso.com" 
$a.Location = "Finance Section" 
$a.ReminderMinutesBeforeStart = 15 
$a.ReminderSet = $True 
  
$result = $a.Save() 

$a = $o.CreateItem($olAppointmentItem) 
  
$a.Start = "4/18/2013 8:00 AM" 
$a.Duration = 60 
$a.Subject = "Monthly Budget Meeting" 
$a.Body = "See Agenda Items at http://www.contoso.com" 
$a.Location = "Finance Section" 
$a.ReminderMinutesBeforeStart = 15 
$a.ReminderSet = $True 
  
$result = $a.Save() 

#End of Script, you've now added three appointments. 
#Expand to suit your needs and add/delete $a. fields as needed.


Deleting Outlook Calendar Appointments


#Connect to Outlooks Calendar Folder System

Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)

#Now you can search for Calendar Objects just like any other type
#In the example below, I'm searching for entries with a 
#subject of Monthly Budget Meeting that starts as of the 
#current date andd time

$folder.items |?{$_.Subject -like "*Monthly Budget Meeting*" -and $_.Start -gt (get-date)}|%{$_.delete()}

#End of script.  Now you know how to search and 
#delete multiple appointments

No comments: