finding attachments older that certain date

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Windows LiveUser132 Windows LiveUser132 10 months, 1 week ago.

  • Author
  • #32835
    Profile photo of Windows LiveUser132
    Windows LiveUser132

    i have this line of code on a script I found at . 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() }

  • #32864
    Profile photo of Tim Pringle
    Tim Pringle

    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.



  • #32932
    Profile photo of Windows LiveUser132
    Windows LiveUser132

    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);

You must be logged in to reply to this topic.