Submitted By: Kent Finkle

Save all email messages from the Outlook Inbox that are more than one month old to a specific folder.

PowerShell
Edit|Remove
$comments = @'
Script name: Save-Emails.ps1 
Created on: Friday, August 10, 2007 
Author: Kent Finkle 
Purpose: How can I use Windows Powershell to 
Save Emails That Are More Than One Month Old? 
'@
 
$olFolderSentMail = 5 

$olMSG = 3 
 
$o = new-object -comobject outlook.application 
$n = $o.GetNamespace("MAPI") 
$f = $n.GetDefaultFolder($olFolderSentMail) 
 
$dt = (get-date).AddDays(-30).toshortdatestring() 
$dt 
 
$Items = $f.Items 
$Filtered = $Items.Restrict("[CreationTime] < '" + $dt + "'") 
 
foreach ($Item In $Filtered) { 
    $a = $Item.Subject 
    $a = $a -replace(":", "") 
    $a = $a -replace("/","") 
    $a = $a -replace("\\","") 
    $a = $a -replace(",","") 
    $a = $a -replace([char]34,"") 
    $a = $a -replace([char]39,"") 
    $a = $a -replace("\?","") 
    $a = $a -replace("\*","") 
    $a = "C:\Test\" + $a + ".msg" 
    $a 
    $Item.SaveAs($a, $olMSG) 
}