none
Powershell script to read Inbox of a shared mailbox and move items to subfolders RRS feed

  • Question

  • Hi Experts

    I am trying to figure out how to read the Inbox of a shared mailbox connected to a user's profile and move mail items to subfolders in the shared mailbox. It seems to be working fine when managing the user's mailbox - but for the shared mailbox I cannot get it to work.

    The first script I have tried I am using the mailbox stores - but that does not seem to allow the GetDefaultFolders function:

    Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
    $outlook = New-Object -ComObject Outlook.Application
    $namespace = $outlook.GetNamespace('MAPI')
    $store = $namespace.Stores['Mailbox - My Mailbox']
    $rootFolder = $store.GetRootFolder()
    
    foreach ($item in $rootFolder.Folders) {
          $item.Name
    }

    The code above will list all the folder in the root of the shared mailbox without any problems. However I cannot see how to get the "Inbox" folder in order to loop through mail items and move them to a subfolder in \Inbox\MySubfolder.

    I have also tried leaving out the $store and instead create a recipient and then use the GetSharedDefaultFolder:

    $recipient = $namespace.CreateRecipient('mymailbox@domain.com')
    $recipient.Resolve()
    $inbox = $namespace.GetSharedDefaultFolder($recipient, [Microsoft.Office.Interop.Office.OlDefaultFolders]::olFolderInbox])

    But that returns an error: Cannot find an overload for "GetSharedDefaultFolder" and the argument count: "2".

    Finally I found another script part that can read the mail items in the Inbox of a shared mailbox - but I am not sure how to move the mail items to a subfolder:

    $class = @"
    using Microsoft.Office.Interop.Outlook;
    public class MyOutlook {
       public MAPIFolder GetInbox(string mailboxName) {
          Application oOutlook = new Application();
          NameSpace oNamespace = oOutlook.GetNamespace("MAPI");
          Recipient oRecipient = oNamespace.CreateRecipient(mailboxName);
          MAPIFolder inbox = oNamespace.GetSharedDefaultFolder(oRecipient, OlDefaultFolders.olFolderInbox);
          return inbox
       }
    }
    "@
    
    Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
    Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook
    $myOutlook = New-Object MyOutlook
    $myInbox = $myOutlook.GetInbox('mymailbox')
    
    foreach ($mail in $myInbox.Items) {
         $mail.Subject
    }
    

    To me it seems that the code defined in the $class object is very similar to the Powershell code above where the GetSharedDefaultFolder is not working. Any reasons why? And will it be possible to move a mail item to a subfolder with the script containing $class?

    Please note that the script should be working with Outlook 2007 (!) and I am not sure if that is why I cannot the get GetSharedDefaultFolder to work.

    Tuesday, August 28, 2018 8:06 AM

Answers

  • That was also my conclusion since I can use the call on a machine with Windows 10 and Outlook 2010 - unfortunately it is not connected to the network so I cannot test it against a shared mailbox.

    Luckily I have just figured out how to get the script with the $class to work. I can simply set the target folder and the run af foreach in the inbox:

    $targetFolder = $myInbox.Folders | Where-Object {$_.Name -eq 'FolderName'}
    
    foreach ($mail in $myInbox.Items) {
    
          $mail.Move($targetFolder)
    
    }

    • Marked as answer by jac.chr Tuesday, August 28, 2018 11:46 AM
    Tuesday, August 28, 2018 11:45 AM

All replies

  • This is all you have to do assuming the shared mailbox is attached and the names are correct.

    $ol = New-Object -ComObject Outlook.Application
    $ns = $ol.GetNamespace('mapi')
    $mb = $ns.Stores['user@domain.com'].GetRootFolder()
    $inbox = $mb.Folders['Inbox']
    $recipient =  $ns.CreateRecipient('mymailbox@domain.com')
    if($recipient.Resolve()){
        $shared = $ns.GetSharedDefaultFolder($recipient, [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
    }else{
        Write-Host 'Unable to resolve'
    }
    


    \_(ツ)_/


    • Edited by jrv Tuesday, August 28, 2018 11:16 AM
    Tuesday, August 28, 2018 8:34 AM
  • It still fails with the error Cannot find an overload for "GetSharedDefaultFolder" and the argument: "2".

    Also I am not quite sure why you have both the $inbox and the $shared object? The mails are in the Inbox of the shared mailbox and should be moved to a subfolder in the shared mailbox as well.

    But thanks anyway.

    Tuesday, August 28, 2018 10:07 AM
  • This line was typed wrong:

        $shared = $ns.GetSharedDefaultFolder($recipient, [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)

    I copied your code and didn't see your mistake.


    \_(ツ)_/

    Tuesday, August 28, 2018 11:16 AM
  • I noticed the error as well and corrected it when running your code part - however the error still occurs…

    Not sure if it makes any difference but it is running on Windows 7 with Office 2007 installed.

    Tuesday, August 28, 2018 11:22 AM
  • What version of Outlook? It works for me.


    \_(ツ)_/

    Tuesday, August 28, 2018 11:24 AM
  • Outlook 2007
    Tuesday, August 28, 2018 11:28 AM
  • I don't think 2007 supports the call.  It likely was added in 2013.  2007 is no longer supported and should not be used for many reasons.


    \_(ツ)_/

    Tuesday, August 28, 2018 11:31 AM
  • That was also my conclusion since I can use the call on a machine with Windows 10 and Outlook 2010 - unfortunately it is not connected to the network so I cannot test it against a shared mailbox.

    Luckily I have just figured out how to get the script with the $class to work. I can simply set the target folder and the run af foreach in the inbox:

    $targetFolder = $myInbox.Folders | Where-Object {$_.Name -eq 'FolderName'}
    
    foreach ($mail in $myInbox.Items) {
    
          $mail.Move($targetFolder)
    
    }

    • Marked as answer by jac.chr Tuesday, August 28, 2018 11:46 AM
    Tuesday, August 28, 2018 11:45 AM
  • Yes.  That is similar to a workaround that I just found that was specific to this issue with 2007.

    Glad you found it.


    \_(ツ)_/

    Tuesday, August 28, 2018 11:48 AM