In this post I am going to share PowerShell script to search mailbox and delete, copy and move searched messages from one mailbox to another mailbox. We can use the exchange powershell cmdlet Search-Mailbox to search a mailbox and copy the results to a specified target mailbox and this cmdlet is available for both Exchange On-Premises and Exchange Online environment.
Before proceed, first we need to connect Exchange Online powershel module by running below commands:
$o365Cred = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365Cred -Authentication Basic -AllowRedirection Import-PSSession $Session
Summary
- Search mailbox based on Subject, Date and From address
- Delete searched messages from mailbox
- Copy messages between mailboxes
- Move messages from source mailbox to target mailbox
Search mailbox based on Subject, Date and From address
The below example searches Alex Wilber’s mailbox for messages that contain the phrase “sales report” in the subject and sent from “[email protected]” in the given date “01/09/2018”, this command return the total number of messages and size of messages returned by the search.
Search-Mailbox -Identity "Alex Wilber" -SearchQuery {Subject:"sales report" AND From:"[email protected]" AND Sent:"01/09/2018"} -EstimateResultOnly -SearchDumpster:$false
Search by date range:
Search-Mailbox -Identity "Alex Wilber" -SearchQuery {Sent:"01/07/2018..01/09/2018"} -EstimateResultOnly -SearchDumpster:$false
Note: By default, the the Search-Mailbox command searches Recoverable Items folder always, to exclude this folder from the search, we need to set the SearchDumpster switch to $false.
Delete searched messages from mailbox
To delete messages we need to use DeleteContent switch, to use the DeleteContent switch you have to be assigned the Mailbox Import Export management role. By default, this role isn’t assigned to any role group. Typically, you assign a role to a built-in or custom role group. Or you can assign a role to a user, or a universal security group. The below example add the role to the Organization Management role group:
New-ManagementRoleAssignment -Name "Import_Export_Organization_Management" -SecurityGroup "Organization Management" -Role "Mailbox Import Export"
Note: You have to create a new Exchange Online PowerShell session to get new role permissions.
This example searches Alex Wilber’s mailbox for messages that contain the phrase “test message” in the subject and deletes the messages from the source mailbox.
Search-Mailbox -Identity "Alex Wilber" -SearchQuery {Subject:"test message" } -DeleteContent
Copy messages between mailboxes
This example searches Alex Wilber’s mailbox for messages that contain the subject “sales report” in the subject and copy the result messages to Allan Deyoung’s mailbox in the target folder “Sales”.
Search-Mailbox -Identity "Alex Wilber" -SearchQuery {Subject:"sales report" } -TargetMailbox "Allan Deyoung" -TargetFolder "Sales"
Move messages from source mailbox to target mailbox
Move operation is nothing but the copy action along with removing messages from source mailbox. This example search and move messages from Alex Wilber’s mailbox to Allan Deyoung’s mailbox.
Search-Mailbox -Identity "Alex Wilber" -SearchQuery {Subject:"sales report" } -TargetMailbox "Allan Deyoung" -TargetFolder "Sales" -DeleteContent
So, search-mailbox has a 10,000 item limit. How do you copy more than 10,000 items? It seems New-MailboxSearch is designed for this, but I can get it to accept the '-TargetMailbox' parameter in Office 365. The microsoft documentation says that some parameters are not supported onprem vs online, but does not specify which. There is also new-compliancesearch, but that does not support "TargetMailbox" either.
Do you have to delete the email? Can you not move the email from inbox to another folder?
You can just exclude the argument -DeleteContent to avoid mail deletion.