Import and export outlook categories with powershell

So I had a neat but complicated script that would import and export categories using the hidden IPM object where outlook stores it.  It was great, but there’s a much more elegant way to do it.  It doesn’t migrate the CategoryID attribute, but that’s used only by the store, and does not seem to be attached to the items or anywhere else.

First you need to connect to the outlook application and MAPI namespace (yes this requires outlook be installed).

$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNamespace(“MAPI”)

Then you can look at all the stores in your profile.  If you don’t limit it to the DisplayName attribute, you’ll get a lot of useless display (i.e. class names).  You can do lots of neat things, like get the default folder of any type (if you know the type name).

$namespace.stores | Select-Object DisplayName
$namespace.GetDefaultFolder(“olFolderCalendar”)

These also work on stores.  So if you want to find the default calendar folder of a store with a given email address/name…

$namespace.stores |
Where-Object { $_.displayname –eq ‘user@mail.local’} |
ForEach-Object {
    $_.GetDefaultFolder(“olFolderCalendar”)
}

On to categories.  The categories list used by an instance of outlook is accessible through the namespace.  namespace.categories returns a collection of categories. 

$namespace.categories | Export-CSV .\outlookCategories.csv

If you have multiple accounts in a given profile, each may have its own collection of categories.  Outlook will change the displayed list for the currently active account, but not expose any way to import or export them.  Luckily, each store also exposes a categories interface.  So you can interact with each one.  To export categories, just save the categories collection to a convenient format, like CSV.

$namespace.stores |
Where-Object { $_.displayname –eq ‘user@mail.local’} |
ForEach-Object {
    $_.Categories
}

The namespace object also exposes a useful method, PickFolder.  It displays all the folders, allows the user to pick, and returns the folder object.

$namespace.PickFolder().Store.Categories

Categories also has an Add method.  It takes the category name (required), color (optional, one of the defined color values), and shortcutkey (optional, from a similar enumeration).  This makes it very easy to copy categories between stores.  Note that if a category with the same name already exists, it will throw a exception, “Value does not fall within the expected range.”  It can be safely ignored, and the target category will exist though it might have a different color or shortcutkey.  This will prompt the user for a target folder, use the store of that folder (or the store if you select the store and not a subfolder), similarly prompt the user for another folder, the source, and add all of the categories from the second to the first.

$sourceStore = $namespace.PickFolder();
$namespace.PickFolder().store.categories |
ForEach-Object {
    $sourceStore.Categories.Add($_.Name,$_.Color,$_.ShortCutKey)
}

You could also import the categories that you just created.

$sourceStore = $namespace.PickFolder();
Import-CSV .\outlookCategories.csv |
ForEach-Object {
    $sourceStore.Categories.Add($_.Name,$_.Color,$_.ShortCutKey)
}

That should do it for us.

Advertisements

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