finding attachments older that certain date

i have this line of code on a script I found at https://gist.github.com/miriyagi/6779410. The original scripts saves the attachment. I removed those lines as I want to delete attachments in Outlook but preserve the email without saving the attachment to a directory. It’s working fine however its deleting all attachments and ignoring the date specified. I am telling it to delete only attachments older than a month. Why is it ignoring the date?

$ErrorActionPreference = “Stop”
[object] $outlook = New-Object -ComObject Outlook.Application
[object] $folder = $outlook.Session.Accounts[1].Session.PickFolder()
[object] $items = $folder.Items
[object] $item = $items.Find("[ReceivedTime] < '" + [DateTime]::Now.AddMonths(-1).ToString() + "' ")

while ($item) {
if ($item.Attachments.Count -gt 0) {
Write-Output "$($item.Subject) ($($item.Attachments.Count))"
[int] $oldSize = $item.Size
while ($item.Attachments.Count -gt 0) {
$attachment = $item.Attachments.Item(1)
$attachment.Delete()
$item.Save()
}

Hey Windows LiveUser132,

From what i can see you’re actually doing your date filtering on the item (i.e. email) itself, as opposed to the attachment date information. $items will be messages, not attachments.

cheers,

Tim

But shouldn’t this line of code below take care of it?
How would I go about filtering on the attachment itself? I want to tell it, any emails older than X, that contain attachments, delete the attachment but preserve the email.

$attachment = $item.Attachments.Item(1);
$attachment.Delete()
$item.Save()