Outlook - Move Old Mail out of Inbox to Folder

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of H Man H Man 1 year, 9 months ago.

  • Author
    Posts
  • #22912
    Profile photo of H Man
    H Man
    Participant

    Hello –

    I am looking to scan my inbox every day and identify old email messages , any messages Identified i'd look to move to a folder called "Older Than 6 Months", which is a sub folder n my inbox.

    Here is what I have so Far. I can get the inbox and have the filter, just not sure hwo to move the email to the "Older Than 6 Months" Folder

    
    $Date = [DateTime]::Now.AddDays(-180)
    
    $Last6Months =  $Date.tostring("MM/dd/yyyy")
    
    
    $o = New-Object -comobject outlook.application
    $n = $o.GetNamespace(“MAPI”)
    
    
    #Pick Which Folder
    $Account = $n.Folders | ? { $_.Name -eq 'jcool@logs.com' };
    $Inbox = $Account.Folders | ? { $_.Name -match 'Inbox' };
    
    
    
    
    $Inbox.Items | ? {$_.senton -ge "$Last6Months"  }  | foreach {
      
      #Need code to move emails here
      
    }
    
    
    
  • #22913
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hey fella,

    If you've got the code for identifying and parsing through the items, it should just be a case of using the Move method for each object.

    $TargetFolder = $Inbox.Folders.Item('Older Than 6 Months')
    
    $Inbox.Items |
    Where-Object -FilterScript {
        $_.senton -ge "$Last6Months"  
    }  |
    ForEach-Object -Process {
        $psitem.Move($TargetFolder)
    }
    
  • #22914
    Profile photo of H Man
    H Man
    Participant

    As always Thanks Tim Ill give this a try

  • #22915
    Profile photo of H Man
    H Man
    Participant

    Tim this is the final script

    
    $Date = [DateTime]::Now.AddDays(-180)
    
    $Last6Months =  $Date.tostring("MM/dd/yyyy")
    
    
    $o = New-Object -comobject outlook.application
    $n = $o.GetNamespace(“MAPI”)
    
    
    
    $Account = $n.Folders | ? { $_.Name -eq 'jcool@logs.com' };
    $Inbox = $Account.Folders | ? { $_.Name -match 'Inbox' };
    $TargetFolder = $Inbox.Folders.Item('Older Than 6 Months')
    
    
    $Inbox.Items |
    Where-Object -FilterScript {
        $_.senton -ge "$Last6Months"  
    }  |
    ForEach-Object -Process {
        $psitem.Move($TargetFolder)
    }
    
  • #22921
    Profile photo of H Man
    H Man
    Participant

    Hey Tim, When i run the script it moves the email but a windows pop up says ," The custom form cannot be opened , outlook will use an outlook form ". Any way to get rid of that?

  • #22922
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Not seen that one before. It's usually a good idea though either not have have the outlook client itself running at the time (otherwise, you're as well to use VBA macros), or if you have to have the client running, to wait for the script start first

  • #22923
    Profile photo of H Man
    H Man
    Participant

    I am actually stoping outlook 1st with this

    if ( [bool](Get-Process OUTLOOK* -EA SilentlyContinue) ) {ps OUTLOOK* | kill -Force ; Start-Sleep -Seconds 10 }

  • #22938
    Profile photo of H Man
    H Man
    Participant

    Hey Tim, i tried it on another computer and it worked fine!. That outlook must be jacked up!

    Thanks Again.. much appreciated!

You must be logged in to reply to this topic.