I had to run a hefty search query for a customer today, searching all mailboxes but every time I tried my command it would error:

Get-Mailbox -ResultSize Unlimited | Search-Mailbox -SearchQuery “((sent:(01/03/2015..09/04/2015)) AND (‘SEARCHTERM1’ OR ‘SEARCHTERM2’ OR ‘ SEARCHTERM3’ OR ‘ SEARCHTERM4’ OR ‘ SEARCHTERM5’ OR ‘ SEARCHTERM6’ OR ‘ SEARCHTERM7’))” -TargetMailbox <MailboxForSearchResults> –TargetFolder SearchRequest-08-04-2015 -loglevel Full

After a bit of searching it seems it’s a syntax error, because we cant search so many mailboxes at once you need to collect all the mailboxes into an array then run the search command on each one:

$Mailboxes = Get-Mailbox -ResultSize Unlimited
Foreach ($Mailbox in $Mailboxes) {
Search-Mailbox -identity $Mailbox -SearchQuery “((sent:(01/03/2015..09/04/2015)) AND (‘SEARCHTERM1’ OR ‘SEARCHTERM2’ OR ‘ SEARCHTERM3’ OR ‘ SEARCHTERM4’ OR ‘ SEARCHTERM5’ OR ‘SEARCHTERM6’ OR ‘ SEARCHTERM7’))-TargetMailbox <MailboxForSearchResults> -TargetFolder SearchRequest-08-04-2015 -loglevel Full
}

 

After that it started searching and bringing back results.

 

Hope it help!

About the author