• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

Need help with Exchange 2010 powershell syntax

I use a script that exports every mailbox to a PST for temporary backup reasons This currently works:

foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -ContentFilter {(Received -lt "04/21/2013")} -FilePath "\\filesserver\PST\$($i.Alias).pst" }

I am trying to figure out a way so I don't have to manually change the date in the received later than switch. Just testing, I was able to get this to work:

New-MailboxExportRequest -mailbox testuser-contentfilter "Received -gt '$startdate'" -filepath \\fileserver\PST\testuser.pst

So I tried this script:

foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -ContentFilter {(Received -lt '$startdate')} -FilePath "\\fileserver\ExmergePST\$($i.Alias).pst" }

I get this error:

The provided ContentFilter value is invalid. ContentFilter is invalid. The value "$startdate" could not be converted to
 type System.DateTime. --> The value "$startdate" could not be converted to type System.DateTime.
    + CategoryInfo          : InvalidArgument: ((Received -lt '$startdate'):String) [], ContentFilterInvalidPermanentE
    + FullyQualifiedErrorId : DD0301EC

 I've tried different syntax changing the delimeters with no success. Any help would be gretly appreciated. Thanks
Mark Lewis
Mark Lewis
1 Solution
Remove the single quote and try..
-ContentFilter {(Received -lt $startdate)}

Open in new window

Single quotes will not expand variables in PowerShell, double quotes will..
QlemoBatchelor and DeveloperCommented:
Further, the difference between
  -ContentFilter {(Received -lt $startdate)}
  -ContentFilter {(Received -lt "$startdate")}
is that the latter will use a string representation of the date - which might lead to errors or unwanted behaviour (the rules for implicit type transformations are complex). The former allows to provide the var contents as DateTime object, which is best.
Mark LewisAuthor Commented:
That did the trick
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.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now