David-Barrett-MS / PowerShell-EWS-Scripts

A collection of PowerShell scripts that use EWS to perform actions against Exchange mailboxes
MIT License
118 stars 13 forks source link

Correct Syntax for date flags #53

Open gmn1211 opened 3 months ago

gmn1211 commented 3 months ago

Hello David,

I have run into an issue when trying to use date flags to filter the messages moved by date. I have been trying to use the -OnlyItemsCreatedBefore flag and have tried using yyyymmdd as well as many other formats for dates but It either finds all of the items in the mailbox or none at all. What is the correct format to filter by dates when calling the script? I am trying to move all emails recieved before 11/01/2023. The call I have been using is below. Thanks.

.\Merge-MailboxFolder.ps1 -SourceMailbox $SourceMailbox -TargetArchive -MergeFolderList @{"Inbox"= "WellKnownFolderName.Inbox"} -ProcessSubfolders -OnlyItemsCreatedBefore 20231101 -CreateTargetFolder -Office365 -OAuth -OAuthTenantId $tenantId -OAuthClientId $clientId -OAuthRedirectUri $redirectUrl

David-Barrett-MS commented 2 months ago

PowerShell dates can be odd, the conversion depends on system locale as far as I am aware. The script simply passes these parameters into the relevant EWS search filter call, so if you provide text rather than a [DateTime], I expect automatic conversion is happening (and failing for some reason).

Try assigning the date to a variable first of all, and sending that to the script as a parameter.

e.g. $today = [DateTime]::today $now = Get-Date