Getting another user’s Outlook folder…


I’ve been working on a small project to analyze report emails I receive under another Exchange account, and hit an issue that has been mentioned all over the internet where calling the GetSharedDefaultFolder MAPI function fails to work in PowerShell. The real issue lies with the fact that the interop version of the Recipient object is automatically unboxed by PowerShell to it’s base COM class. Unfortunately the interop version of GetSharedDefaultFolder can’t box it back up, and neither can PowerShell, so it all fails miserably.

I’ve read quite a few posts on the subject, mostly based on PowerShell v1.0 which have had horribly complex workarounds where you have to build C# modules and then call those from PowerShell… However, in v2.0 we can use the Add-Type command to provide the same effect and with much less work.

First, we need to reference the Outlook interop assembly:

Add-Type -AssemblyName Microsoft.Office.Interop.Outlook

Next we define the new class that will wrap the GetSharedDefaultFolder call. Note the use of the ReferencedAssemblies property on Add-Type to ensure the class will compile:

$class = @”
using Microsoft.Office.Interop.Outlook;public class MyOL
{
    public MAPIFolder GetInbox(string userName)
    {
        Application oOutlook = new Application();
        NameSpace oNs = oOutlook.GetNamespace(“MAPI”);
        Recipient oRep = oNs.CreateRecipient(userName);
        MAPIFolder inbox = oNs.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderInbox);
        return inbox;
    }
}
“@

Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook

Now that we have this, all we have to do is instantiate the new class and get the inbox object back for a given user:

$MyOL = New-Object MyOL
$olInbox = $MyOL.GetInbox(“otherUserName”)

This is fairly simple now and also provides a good basis for further Outlook/PowerShell interop work… Hope this is of use to some of you!

  1. Vin
    December 5, 2012 at 7:23 pm

    What do I need to do if that other users shared folder is on another exchange server?

  2. Mike
    February 11, 2013 at 8:29 pm

    hello, I can not get this powershell script to work. (I’m running it in Exchange management shell (powershell v2))

    I get the error message below

    “c:\users\mike\appdata\local\temp\wpidlxir.0.cs(6) : unexpected character ‘”‘ message

    I received the error at the point where I run the command below:

    Add-type $class -referencedassemblies microsoft.office.interop.outlook

    This script is exactly what I am looking for since I ran into the same issue with the getshareddefaultfolder function. Any assistance is appreciated.

    • February 12, 2013 at 1:30 pm

      Hi Mike,

      When you copy the code from the site, it uses incorrect coding for the double quotes. Just delete and re-add all the double quotes (“) inside the ISE or your favourite text editor and all will work fine.

      I have tested this in PowerShell v3 to make sure.

      Thanks,
      Chris.

  3. Joe
    July 23, 2013 at 8:15 pm

    How would you then access a subfolder under their inbox?

  1. No trackbacks yet.

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

%d bloggers like this: