Export Mailbox Content in Date Range with non US Locale

PowerShell Export Mailbox

In this  article I have described how you can use PowerShell to export mailbox content in a date range but as correctly user Gavin commented:

This doesn’t work if you are using any other regional date format than the US format.

The statement is absolutely accurate as in the post I forgot to mention that no matter the locale settings configured for the server PowerShell will internally expect dates in the US format, so trying to export mailbox with any other date format will result in an error message.

There are two different approaches you can use to workaround this design limitation, let’s explore both of them.

Export Mailbox Content for non US Locale – Method 1

The easiest and most common proposed solution is to change the Server’s Regional Settings to English (United States)

Regioan and Language Settings

With the configuration in place the export filter would simply be similar the following

New-MailboxExportRequest -ContentFilter {(Received -lt '01/01/2015') -and (Received -gt '11/30/2014')} -Mailbox info -FilePath \srv-ex2k13mb01x$info_Export_December2014.pst

Export Mailbox Content for non US Locale – Method 2

Changing Regional Settings and dates format has a server wide impact and it’s not always practical or even possible to do so. Luckily we can “trick” PowerShell into thinking the right culture and date formats are being used.

I use the following to export mailbox content for the previous month on one of my servers with CH-DE locale

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us") # This will force the correct date culture

Add-PSSnapin *exchange*

$start = (Get-date -Month 4 -Day 30 -Hour 11 -Minute 59 -Second 59)
$end = (Get-Date -Month 6 -Day 1 -Hour 00 -Minute 00 -Second 00)

$dateFilter= "(Received -gt '"+$start+"') -and (Received -lt '"+$end+"')"
new-Mailboxexportrequest -mailbox SOMEMAILBOX -ContentFilter $filter -filepath C:SOME.pst

New-MailboxExportRequest -Mailbox ExportMbx -ContentFilter $dateFilter-FilePath  \SZHEXMB01x$ExportPST_Export_May2016.PST

The $dateFilter variable in the above example would contain the following

(Received -gt '04/30/2016 11:59:59') -and (Received -lt '06/01/2016 00:00:00')

There is one last thing that needs to be kept in mind the above will work in  standard PowerShell session, not EMS, as if you run  the above in an Exchange Management Shell session the mailbox export will fail and the following message with event ID 1100 will be displayed in the Application event viewer

ContentFilter is invalid. The value "30.04.2016 11:59:59" could not be converted to type System.DateTime. --> The value "30.04.2016 11:59:59" could not be converted to type System.DateTime.

I did not dig deep into the error but I think this has to do with the fact that in EMS specific cmdlets are immediately loaded and the date format culture is ignored.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s