none
[OUTLOOK] Moving emails from Inbox folder to another folder using Powershell RRS feed

  • General discussion

  • Hi all,

    I try to move emails from indbox folder to another subfolder.

    So, below is the script which used to do the job:

    $outlook = New-Object -ComObject Outlook.Application; 
    $olFolderInbox = 6;
    $ns = $outlook.GetNameSpace("MAPI"); 
    $inbox = $ns.GetDefaultFolder($olFolderInbox);
    $mailboxFolders=$inbox.Folders| where-object { $_.name -eq "test" }
    
    $MoveTarget = $inbox.Folders.Item("test").folders.Item("done") 
    $mailboxFolders.Items|foreach{
    [void]$_.Move($MoveTarget)
        }

    This works but works bad.

    In fact only half items are moved into the subfolders. For exemple if I have 6 mails, only 3 mails are moved from test to done.

    Please can anyone suggest what is wrong & where?

    Many Thanks,

    • Moved by Bill_Stewart Thursday, May 24, 2018 12:44 PM Move to more appropriate forum
    Thursday, May 24, 2018 11:35 AM

All replies

  • You are modifying the collection that you are iterating over. Instead of the "foreach" loop, use a backward "for" loop from Items.Count down to 1.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, May 24, 2018 1:21 PM
  • Thank you Dmitry.

    I've modify my code with "for" :

        $MoveTarget = $inbox.Folders.Item("test").folders.Item("done") 
        for ($c = 1 ; $c -le $mailboxFolders.Items.count ; $c++) {
            [void]$mailboxFolders.Items.Move($MoveTarget)
        }

    Count seems to be ok but I've the message :

    Method invocation failed because [System.__ComObject] does not contain a method named 'Move'.
    At C:\Users\ABC\Documents\check.ps1:63 char:9
    +         [void]$mailboxFolders.Items.Move($MoveTarget)
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Move:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound





    • Edited by RTX8 Monday, May 28, 2018 12:55 PM
    Monday, May 28, 2018 12:50 PM
  • Backward "FOR" loop means count backwards fro "Count" to 1 using $c--

    for ($c = $mailboxFolders.Items.count ;$c -ge 1;$c--) {


    \_(ツ)_/

    Monday, May 28, 2018 1:23 PM
  • Thank you Jrv.

    Now I've this code :

        $MoveTarget = $inbox.Folders.Item("test").folders.Item("done") 
        for ($c = $mailboxFolders.Items.count; $c -ge 1;$c--) {
            [void]$mailboxFolders.Items.Move($MoveTarget)
        }

    and result is :

    Method invocation failed because [System.__ComObject] does not contain a method named 'Move'.
    At C:\Users\ABC\Documents\check.ps1:63 char:9
    +         [void]$mailboxFolders.Items.Move($MoveTarget)
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Move:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound


    Monday, May 28, 2018 3:05 PM
  • You didn't use the index.  You have to index I to the items.

    $moveTarget = $inbox.Folders['test'].Folders['done'] 
    for ($c = $mailboxFolders.Items.count; $c -ge 1;$c--) {
        [void]$inbox.Folders['test'].Items[$c].Move($moveTarget)
    }



    \_(ツ)_/

    Monday, May 28, 2018 3:47 PM
  • Hello,

    Ok it works with your last code posted!

    Thank all !

    Tuesday, May 29, 2018 12:46 PM