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!
What do I need to do if that other users shared folder is on another exchange server?
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.
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.
How would you then access a subfolder under their inbox?
I’m hoping to leverage this page I found to do exactly that, Joe
https://blogs.technet.microsoft.com/heyscriptingguy/2006/06/16/how-can-i-write-a-script-that-accesses-all-the-subfolders-in-my-outlook-inbox/
Seems like once you have the $inbox variable you could throw it into some of this code.