SOLVED

Search emails last month with powershell

Copper Contributor

Hi,

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:"blabla.com") 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 (Copper Contributor)
Solution

 

@Kali0 

Try this and let me know if it works

 

$FromDate = (Get-Date).AddMonths(-2).ToShortDateString()
$ToDate = (Get-Date).ToShortDateString()
$Query = '{(From:"blabla.com") 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:"fca.org.uk") AND (Received:' + $FromDate + '..' + $ToDate + ")"
Search-Mailbox -Identity fcirulli -SearchQuery $Query -Targetmailbox "pgross" -Targetfolder "TEST" -Loglevel Full
1 best response

Accepted Solutions
best response confirmed by Kali0 (Copper Contributor)
Solution

 

@Kali0 

Try this and let me know if it works

 

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

 

View solution in original post