Create a calendar item

Continuing the occasional look at Outlook automation its time to see how we create a calendar item

function new-calendaritem {            
param (            
            
 [string]$mailbox,            
             
 [datetime]$start,            
             
 [datetime]$end,            
             
 [string]$subject,            
             
 [string]$location,            
             
 [string]$body            
            
)            
$outlook = New-Object -ComObject Outlook.Application            
            
$folder = get-mailfolders |             
where {$_.Path -like "*calendar*" -and $_.Path -notlike "*birthday*" -and $_.Path -like "*$mailbox*"}            
            
$calendar = $outlook.Session.GetFolderFromID($folder.EntryID, $folder.StoreID)            
             
$entry = $calendar.Items.Add(1)            
$entry.Start = $start            
             
$entry.End = $end            
             
$entry.Subject = $subject            
             
$entry.Location = $location            
             
$entry.Body = $body             
             
$entry.Save()            
            
}

 

The appropriate calendar is identified – remember I have 4 to choose from. The Add method is used from the calendar items collection and the properties populated.

I always enter dates like this “14 September 2011 11:00” as it removes any problems with culture. In the UK this would be “14/9/2011 11:00” but when entering in this style I have to remember to use the US format “9/14/2011 11:00”

The mailbox parameter helps identify which calendar the entry goes

Advertisements
This entry was posted in COM, Networking, Outlook, PowerShell V2. Bookmark the permalink.

5 Responses to Create a calendar item

  1. Jeremiah says:

    Where does the “get-mailfolders” command come from? Are you importing a module to get access to it? I get the following error ” The term ‘get-mailfolders’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.”.

  2. Pingback: PowerShell add Holidays to Outlook Calendar | SharePointed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s