Outlook Automation in PowerShell - Calendar Scrubbing
Thu, Dec 14, 2006
2-minute read
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