Powershell Script to delete all emails before 1/1/17

I would like a powershell script that deletes all emails older than January 1st, 2017 in microsoft exchange
Khari TurnerAsked:
Who is Participating?
 
Jose Gabriel Ortega CCEO Faru Bonon ITCommented:
For outlook, you would need a COM object that opens your outlook and then do your query and removal

#sources: 
#https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/26/use-powershell-to-data-mine-your-outlook-inbox/
#https://serverfault.com/questions/44528/find-all-emails-in-outlook-inbox-which-are-undelivered-with-powershell

add-type -Assembly "Microsoft.Office.Interop.Outlook" | Out-Null
$BaseDate= New-Object System.DateTime -ArgumentList 2018,3,29 #Jan 1 2017 YYYY,MM,DD

$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application

$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)

$s1=$false
$s2=$false
$se=$false

#$Allitems = 
foreach( $item in $folder.items){

    try{
        $time = $item | select -ExpandProperty ReceivedTime -ea Stop
        if($time -lt $BaseDate){
            #run it like this 1st and test if the subjects are correct.
            write-host "$time < $BaseDate -> $($item.Subject)"
            #Once you verify that remove the has in the line below to delete emails.
            #$email.delete()
        }
        else{
            Write-Host "$time > $BaseDate"
            if(!$s1 -and $time -lt $BaseDate ){
                $s1=$true
            }
            elseif(!$s2){
                $s2=$true
            }
            elseif(!$s3){
                $s3=$true
            }

            if($s1 -and $s2 -and $s3){
                break;
            }
        }
    }
    catch{
        Write-Warning "There was an error while processing the email with a message: $time ->$($_.Exception.Message)"
    }
 
 }

Open in new window


1st run it like this you will get the message on line 25, and line 27 is the "delete order" so, when you are absolutely sure, remove the hash on that line.

This will work for the "Inbox" root if you want to remove it from all folders you'd need to combine my answer with:
https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/26/use-powershell-to-data-mine-your-outlook-inbox/
https://serverfault.com/questions/44528/find-all-emails-in-outlook-inbox-which-are-undelivered-with-powershell

And that goes beyond... So I'd suggest a gig.
0
 
Dorababu MSenior Software EngineerCommented:
Try this by changing the filter as per your requirement

Get-Mailbox -Identity “USERHERE” | Search-Mailbox -ContentFilter {Size -gt 1024KB} -and {((Received -lt ’12/06/2010) -and (Received -ge ’01/01/2013')) -DeleteContent -force

Search-Mailbox -Identity <username> -SearchQuery '(Received:09/20/2012..10/12/2012)' -deletecontent

Open in new window

1
 
Jose Gabriel Ortega CCEO Faru Bonon ITCommented:
What version of exchange are you?
Jose
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Khari TurnerAuthor Commented:
My version is 14.3.361.4001, so i think 2010
0
 
Jose Gabriel Ortega CCEO Faru Bonon ITCommented:
Yes to be exact: Update Rollup 18 for Exchange Server 2010 SP3
Source: https://technet.microsoft.com/en-us/library/hh135098%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396

Do you need to delete the emails in all mailboxes? or just in one specific one?.

jose
1
 
Khari TurnerAuthor Commented:
Yes I need to delete ALL of the mailboxes of all files that predate 1/1/2017. I just tried:
Get-Mailbox -Identity “User” | Search-Mailbox -identity "User" -SearchQuery 'Received<01/24/2018' -DeleteContent -force

Just to Test and see if I could delete some emails from my mail box that predate January 24th but I get the error:

Get-Mailbox : The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
0
 
Jose Gabriel Ortega CCEO Faru Bonon ITCommented:
Yes, that's why I asked the version of exchange.
That command should work, are you using the powershell command for exchange?
Source:https://technet.microsoft.com/en-us/library/bb123685(v=exchg.141).aspx
jose
1
 
Khari TurnerAuthor Commented:
Yes I am, I'm trying to use it for Microsoft Outlook
0
 
Jose Gabriel Ortega CCEO Faru Bonon ITCommented:
I mean you need to run the script on the Exchange Console for 2010.
https://technet.microsoft.com/en-us/library/bb123778(v=exchg.141)
2
 
Jose Gabriel Ortega CCEO Faru Bonon ITCommented:
done
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.