Hi I am working on script to save emails to database and delete them using example https://sqlnotesfromtheunderground.wordpress.com/2014/08/14/delete-emails-older-than-x-days-from-outlook-specific-folder-with-powershell/My delete code $MailBox = "SQLDBA@MYDomain.com"$Subfolder = "DBA_ALERTS"# Outlook Connection$Outlook = New-Object -ComObject Outlook.Application# Outlook folder to clean$EmailsInFolder = $Outlook.Session.Folders.Item($MailBox).Folders.Item($Subfolder).Items $OlderThenDays = 1$Today = get-date$counter = 0$EmailsInFolder.countforeach ($email in $EmailsInFolder){ $difference = New-TimeSpan -Start $email.SentOn -End $Today if ($difference.Days -ge $OlderThenDays) { $difference.Days $email.SentOn $email.Subject $email.Delete()$counter ++ } }Write-Host $countercode is working but does not delete all selected emailfolder contain 500+ email meeting condition but foreach loop does not delete all selected emails at once I have to run code several times and each time it delete different amount of emails in decreasing order 205,96,4815Do I have to call some other metod or set specific property in object in order to delete all selected emails at once ?
↧