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()
}