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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 CastroCEOCommented:
What version of exchange are you?
Jose
0
Khari TurnerAuthor Commented:
My version is 14.3.361.4001, so i think 2010
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

Jose Gabriel Ortega CastroCEOCommented:
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 CastroCEOCommented:
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 CastroCEOCommented:
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 CastroCEOCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jose Gabriel Ortega CastroCEOCommented:
done
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.