Powershell – Exchange’s New-MailboxExportRequest behaves strangely with DateTime parameters to ContentFilter

exchange-2010powershell

I am attempting to extract a certain date-range of emails from a mailbox into a PST, but using a DateTime parameter for ContentFilter doesn't seem to work as expected.

This should export everything for the last 5 days:

$endDate = Get-Date "00:00:00"
$startDate = $endDate.AddDays(-5)

Write-Host "Exporting items between $startDate and $endDate..."

New-MailboxExportRequest -ContentFilter {(Received -ge $startDate) -and (Received -lt $endDate)} -Mailbox "EmailLog" -FilePath "\\ReadyNAS\backup\Mailboxes\EmailLog\EmailLog.pst"

But the PST ends up with the full 15+Gb of the mailbox in it.

If I specify the dates manually, it works fine:

New-MailboxExportRequest -ContentFilter {(Received -ge "01-06-2013 00:00:00") -and (Received -lt "06-06-2013 00:00:00")} -Mailbox "EmailLog" -FilePath "\\ReadyNAS\backup\Mailboxes\EmailLog\EmailLog-man.pst"

I'm not sure if this is due to some sort of regional-settings conflict, but I would have thought that passing a typed parameter would avoid that sort of sillyness. The reason I suspect this is because if I print out the dates (the Write-Host in the first example) the dates come out in US format:

[PS] C:\Windows\system32>Write-Host "Exporting items between $startDate and $endDate..."
Exporting items between 06/01/2013 00:00:00 and 06/06/2013 00:00:00...

Best Answer

I confirm I have solved this by specifying the content filter exactly as "(Received -ge '$startDate') -and (Received -lt '$endDate')" to prevent it from becoming $null with help from this question.

My client's Exchange Server 2013 version/build no. is 15.0.847.32.

It could be a rare condition, still I added my answer here just in case it helps someone.

Edit: Based on the concept, I have written a PowerShell Script to archive Journal Mailbox monthly with email reporting.