Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Script to clean out old messages from Journal mailbox after exporting email to .PST

$
0
0

After using the following script to export email to a .PST file I'm wanting to clean out the Journal email box called "JournalMailbox":  New-MailboxExportRequest JournalMailbox -FilePath '\\dfsdr.dc.pud\PowershellEmailArchive\Journal.pst'

I'm trying to then clean out all older emails in the "JournalMailbox" by using SearchQuery with the dates being a variable.  Here are the variables:

$startDate = (Get-Date).AddDays(-31).ToString('MM/dd/yyy')

$endDate = (get-date).AddDays(-1).ToString('MM/dd/yyy')

If I run it the following way it works but I have to change the dates manually:

Search-Mailbox -Identity JournalMailbox -TargetFolder inbox -TargetMailbox jimk -SearchQuery "Received:02/07/2014..03/09/2014" -DeleteContent -Force

I've tried running it the following ways and I still get errors:

Get-Mailbox -Identity "JournalMailbox" | Search-Mailbox -SearchQuery "Received:'${startDate}'..'${endDate}'" -DeleteContent -Force

Search-Mailbox -Identity JournalMailbox -TargetFolder inbox -TargetMailbox jimk -SearchQuery "Received:'${startDate}'..'${endDate}'" -DeleteContent -Force

Search-Mailbox -Identity JournalMailbox -TargetFolder inbox -TargetMailbox jimk -SearchQuery "Received:'${$startDate}'..'${$endDate}'" -DeleteContent -Force

Search-Mailbox -Identity JournalMailbox -TargetFolder inbox -TargetMailbox jimk -SearchQuery "Received:"$startDate".."$endDate"" -DeleteContent -Force

Search-Mailbox -Identity JournalMailbox -TargetFolder inbox -TargetMailbox jimk -searchQuery "Received:>$startDate and Recieved:< $enddate" -DeleteContent -Force

Any suggestions with be greatly appreciate!  Thanks in advance.   Jim


Viewing all articles
Browse latest Browse all 6937

Trending Articles