none
Powershell : Moving mails from specific sender (XYZ) from inbox ( folder- important) to another folder(GEN-NEXT) RRS feed

  • Question

  • I have a folder in oulook with many senders, i want to move the specific sender mails to new folder called "GEN-NEXt", code is below, i get an error "Cannot convert the 
    "System.Object[]" value of type "System.Object[]" to type "System.Int32".""

    $olFolderInbox = 6
    $ol = new-object -comobject "Outlook.Application"
    $mapi = $ol.getnamespace("mapi")
    $inbox = $mapi.GetDefaultFolder($olFolderInbox)
    $msgs = $inbox.Folders.Item("important")
    $msgdetails = $msgs.items | Select-Object SenderName, SenderEmailAddress -unique
    if($lic = ($msgdetails.sendername -match "Jose, sajen"))
    {
    [void]$msgdetails.item($lic).move($inbox.Folders.Item("GEN-Next"))
    }


    • Edited by shak.mehak Friday, March 23, 2018 6:25 AM
    • Moved by Bill_Stewart Wednesday, May 9, 2018 2:48 PM This is not "fix/debug/rewrite my script for me" forum
    Friday, March 23, 2018 6:23 AM

All replies

  • You must move mail items and not objects derived from mail Items.  This means you cannot use "Select-Object".  Use "Where-Object" to filter the items then move them in a loop.

    $msgs.items | 
        Where{$_.sendername -match 'Jose, sajen'} |
        ForEach{$_.Move($inbox.Folders['GEN-Next'])} 


    \_(ツ)_/


    • Edited by jrv Friday, March 23, 2018 9:20 AM
    Friday, March 23, 2018 6:32 AM
  • Sorry.  I copied it wrong.  The example is fixed so try again.  You have to think about and understand what the code is doing.

    \_(ツ)_/

    Friday, March 23, 2018 9:21 AM