2

With PowerShell, I'm trying to write a script that will move an email from my inbox to a folder, once I have finished doing what I want with it. This is what I have so far.

$folder = $namespace.GetDefaultFolder(6)
$filepath = "C:\Users\Documents\PowerShell"
$folder.Items| foreach {
        $_.attachments|foreach {
            $filename = $_.filename
                If ($filename.Contains("test.xls")) {
                        $_.saveasfile((Join-Path $filepath $filename))
                        Rename-Item -LiteralPath '.\test.xls' -NewName "Server.xls"
                        #File move code should go here
                        }

                If ($filename.Contains("test2.xls")) {
                        $_.saveasfile((Join-Path $filepath $filename))
                        Rename-Item -LiteralPath '.\test2.xls' -NewName "Workstation.xls"
                        #File move code should go here
                        }       
        }
}

Right now I have it set up so that it will search my inbox for any email containing .xls attatchments with a certain name, rename the .xls attachment, and save it to a specified folder in my documents. Now, once I'm done with that, I want to move the original email containing the attachment to a folder within my Outlook email called "test folder". I've seen a few examples online of people doing something similar to this, but nothing seems to be working for me. Any advice on how to do this?

JKelley
  • 69
  • 1
  • 1
  • 7

1 Answers1

2

You need to use the Move method which moves a Microsoft Outlook item to a new folder.

To find the required folder you can iterate trough them using the Folder.Folders property. See How to: Enumerate Folders for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I'm using the move method to archive mails from inbox to another folder. But the mails are being archived only when Outlook is close. When Outlook is open, mails do not move to the other folder. Can you help me with the reason for this strange behaviour or suggest a way to fix this so that mails can be archived when outlook is open as well. I used this forum to understand moving of mails. https://social.msdn.microsoft.com/Forums/en-US/20244f17-a807-4198-8f59-5f5210bb17cd/outlook-moving-emails-from-inbox-folder-to-another-folder-using-powershell?forum=outlookdev – visleck Mar 23 '21 at 18:38
  • Move and archive are different actions. – Eugene Astafiev Mar 23 '21 at 20:07
  • I am really sorry for the miscommunication, I meant that I wanted to move the mails to a different folder, but that is not working when outlook is open. Mail moves to the desired folder only when outlook is close. – visleck Mar 24 '21 at 15:10
  • What code do you use for moving items in Outlook? How and when do you run the code? – Eugene Astafiev Mar 25 '21 at 08:19