0

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!

Svb
  • 1
  • 2
  • When you enter the date manually, you're using `-` as the delimiter. Your `Read-Host` asks for `/` as the delimiter. Try if that makes a difference. – Tomalak Feb 21 '22 at 09:41
  • According to [the docs](https://learn.microsoft.com/en-us/powershell/module/exchange/search-mailbox?view=exchange-ps#parameters), `-SearchQuery` should be a **string**, not a scriptblock. Also I believe the format of the date should be either `dd-MM-yyyy` (localized) OR `MM/dd/yyyy` (international). Try `$date = Read-Host "Enter the date (dd-MM-yyyy)"; Search-Mailbox -Identity "abc@consto.onmicrosoft.com" -SearchQuery "received<=$date" -DeleteContent -Force` – Theo Feb 21 '22 at 09:57
  • @Tomalak I tried it , its not working – Svb Feb 21 '22 at 10:41

0 Answers0