Search emails last month with powershell

New Contributor


I try to search emails received from a specific sender during the last two months :

Here my request but It doesn't work on Exchange 2019 :


$fromDate = (Get-Date).AddMonths(-2).ToShortDateString()
$toDate  = (Get-Date).ToShortDateString()
Search-Mailbox -identity blabla -SearchQuery {(From:"") AND (Received:$fromDate..$toDate)' -Targetmailbox "blabla" -Targetfolder "TEST" -Loglevel Full 



Could you please tell me how to put variables on the request ?


Thank you for your help

2 Replies
best response confirmed by Kali0 (New Contributor)



Try this and let me know if it works


$FromDate = (Get-Date).AddMonths(-2).ToShortDateString()
$ToDate = (Get-Date).ToShortDateString()
$Query = '{(From:"") AND (Received:' + $FromDate + '..' + $ToDate + ")}"
Search-Mailbox -Identity blabla -SearchQuery $Query -Targetmailbox "blabla" -Targetfolder "TEST" -Loglevel Full


Hi Surajbudhani

Thank you very for your help, it works !!

$FromDate = (Get-Date).AddMonths(-2).ToShortDateString()
$ToDate = (Get-Date).ToShortDateString()
$Query = '(From:"") AND (Received:' + $FromDate + '..' + $ToDate + ")"
Search-Mailbox -Identity fcirulli -SearchQuery $Query -Targetmailbox "pgross" -Targetfolder "TEST" -Loglevel Full