Outlook Automation in PowerShell – Calendar Scrubbing

Here's one thing you might have noticed happening to your Outlook calendar. As time goes on, the calendar titles get obscured by the ugly meeting barnacles such as "FW: " and "Updated: ". I don't know anybody that cares about that meta-data, but it lives on -- and I'd like to remove it.

Omar Shahine mentioned that he wrote an Outlook addin to do this once. Since I've been doing some mail management tasks with PowerShell lately (more on that in future posts,) I thought it might be a useful thing to demonstrate via Outlook's Outlook.Application COM object.

The script below is quite simple. Run "Set-CalendarTitles," watch the pretty progress bar, and enjoy your newly clean calendar.

##############################################################################
## Set-CalendarTitles.ps1
##
## Clean calendar titles to remove:
## "FW: ", "Updated: "
##############################################################################

function main
{
    ## Create the outlook application object, and connect to the calendar
    ## folder
    $olApp = new -com Outlook.Application
    $namespace = $olApp.GetNamespace("MAPI")
    $fldCalendar = $namespace.GetDefaultFolder(9)

    "Gathering calendar items"
    $items = $fldCalendar.Items

    ## Visit each item, updating progress as we go
    $counter = 0
    foreach($item in $items)
    {
        $status = "Processing item {0} of {1}: {2}" -f $counter,$items.Count,$item.Subject
        Write-Progress "Processing calendar items" $status -PercentComplete ($counter / $items.Count * 100)

        ## Remove the extra text
        cleanItem $item "FW: "
        cleanItem $item "Updated: "

        $counter++
    }
}

## Clean the title of a calendar entry if it matches
## searchString
function cleanItem($item, $searchString)
{
    if($item.Subject -match $searchString)
    {
        $item.Subject = $item.Subject -replace $searchString,""
        $item.Save()
    }
}

. main

5 Responses to “Outlook Automation in PowerShell – Calendar Scrubbing”

  1. Steve Schreiber writes:

    Love the examples. However, I’ve recently tripped up on a problem with even trying to invoke a reference to the Outlook COM interface. I’ve used the lines "$olApp = new -com Outlook.Application" and "$olApp = New-Object Microsoft.Office.Interop:Outlook". Both fail. The errors are, "New-Object : Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005." and "New-Object : Cannot find type [Microsoft.Office.Interop:Outlook]: make sure the assembly containing this type is loaded", respectively.

    Any thoughts? I’m on Outlook 2007 and Vista. I’ve checked to ensure that .net programmability for Outlook is installed. Damned if I can figure out a next step.

    [Not sure if you’ll even respond, as this is such an old post, but what the heck.]

  2. Lee writes:

    Hi Steve;

    I think you might be having a problem with the colon between Interop and Outlook. Here’s an example of somebody using the interop libraries: http://www.muscetta.com/2007/05/30/death-by-right-click-delete-nope-powershell/

    Lee

  3. Steve Schreiber writes:

    Hey Lee – Thanks for the suggestion but continues to "not work". Of note, neither the method your scripts typically use ("-com Outlook.Application") nor the interop assemblies method work. I’m assuming that there is no other Powershell configuration work I need to do to make this function, correct?

    Interestingly, the suggested line, in the link you reference, "[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.Office.Interop.Outlook’)" does succeed. Unfortunately, any instantiations of a new outlook object reference after it still do not work (I was naively hoping that this line might help Powershell "see" the assemblies.

    Also of note, I *can* use the same API’s in WSH scripts (using the "new ActiveXObject(‘Outlook.Application’)" technique). Damn confusing.

  4. Steve Schreiber writes:

    Closing this out – I figured it out. I had my Powershell launching with run as Administrator privileges. Once this was disabled, all was well. Thanks for the help.

  5. Lee writes:

    Hmm, that’s a surprising cause. Were you launching it actually as the Administrator user (which might not have access to your profile?)

Leave a Reply