Forum Discussion

Kali0's avatar
Kali0
Copper Contributor
Jan 03, 2022

Search emails last month with powershell

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

  •  

    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

     

  •  

    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

     

    • Kali0's avatar
      Kali0
      Copper Contributor
      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

Resources