Delete mails that have been received before the date given by the user using powershell

Svb 71 Reputation points
2022-02-21T09:22:56.09+00:00

Hello,
I am unable to delete mails that have been received before the date given by the user.
The code I used is,
$date = Read-Host "Enter the date (dd/mm/yyyy)"
Search-Mailbox -Identity "abc@consto.onmicrosoft.com" -SearchQuery { received<=$date} -DeleteContent -Force
Its showing error as,
The KQL parser threw an exception.

  • CategoryInfo : InvalidArgument: (:) [], ParserException
  • FullyQualifiedErrorId : [Server=DM4PR12MB5360,RequestId=297f7526-fe64-43ec-916b-cbd2e3c19cb1,TimeStamp=21-02-2022 08:29:53] [FailureCategory=Cmdlet-ParserException] 5E9AB73F
  • PSComputerName : outlook.office365.com

But when I directly use the date without storing it in variable like below it works.
Search-Mailbox -Identity "abc@consto.onmicrosoft.com" -SearchQuery { received<=17-02-2022} -DeleteContent -Force

Can anyone tell why its working when I am directly using the date in cmdlet and not working when stored in variable and used.

Thank you!

Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
4,903 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,197 questions
PowerPoint Management
PowerPoint Management
PowerPoint: A family of Microsoft presentation graphics products that offer tools for creating presentations and adding graphic effects like multimedia objects and special effects with text.Management: The act or process of organizing, handling, directing or controlling something.
221 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

Accepted answer
  1. KyleXu-MSFT 26,211 Reputation points
    2022-02-22T09:49:03.087+00:00

    @Svb

    Are you using Exchange online? I tested it in my lab, there doesn't exist issue with it:

    Search:
    176674-qa-kyle-17-43-24.png

    Delete:
    176792-qa-kyle-17-45-33.png

    I connect to Exchange online in the way of Basic auth.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2022-02-21T16:05:56.887+00:00

    From this web page: keyword-query-language-kql-syntax-reference

    Date or time values for properties
    KQL provides the datetime data type for date and time.The following ISO 8601-compatible datetime formats are supported in queries:

    YYYY-MM-DD

    YYYY-MM-DDThh:mm:ss

    YYYY-MM-DDThh:mm:ssZ

    YYYY-MM-DDThh:mm:ssfrZ

    Maybe there's some behind-the-scenes management of the string based on your date/time locale setting.

    Try this and see if it makes a difference:

    [datetime]$date = Read-Host "Enter the date (dd/mm/yyyy)"