finding attachments older that certain date

Welcome Forums General PowerShell Q&A finding attachments older that certain date

This topic contains 2 replies, has 2 voices, and was last updated by

3 years, 1 month ago.

  • Author
  • #32835

    Points: 0
    Rank: Member

    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

    Points: 60
    Rank: Member

    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

    Points: 0
    Rank: Member

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

The topic ‘finding attachments older that certain date’ is closed to new replies.