Oleg Agadjanyan
Oleg Agadjanyan

Reputation: 60

Exchange online - move messages with powershell

I have a huge mailbox (~50Gb) with plenty of messages in 'Inbox' . I'm looking for moving certain messages received before particular date to another folder at the same mailbox. I have tried to do it from Outlook Windows app but it seems too slow and I can't do it for all the messages at once. Outlook just crash. Is there any way to perform the task from Exchange Powershell ? I can certainly create server side rule but how to apply it to the messages already in 'Inbox' ?

New-InboxRule -Name Testmove2018 -Mailbox test -MoveToFolder "MailboxName:\2018" -ReceivedAfterDate "09/01/2015"

Oleg

Upvotes: 0

Views: 7630

Answers (1)

Cpt.Whale
Cpt.Whale

Reputation: 5341

The Exchange Online powershell module still allows you to copy and delete messages matching a search with Search-Mailbox, but you must copy them elsewhere first. Use an empty mailbox:

# First, check your search filter using EstimateOnly or LogOnly:
Search-Mailbox [email protected] -SearchQuery 'Subject:Move Me' -EstimateResultOnly

# Copy the items to a temp mailbox and delete from the primary
# (EXO does not allow copying search results to same mailbox)
# param block just for readability:
$Params = @{
  Identity      = '[email protected]' 
  SearchQuery   = 'Subject:Move Me' 
  TargetMailbox = '[email protected]' 
  TargetFolder  = 'Temp' 
  DeleteContent = $true
}
$TempResult = Search-Mailbox @Params

# Now move back to original mailbox
$Params.Identity      = '[email protected]'
$Params.TargetMailbox = '[email protected]'
$Params.TargetFolder  = 'Moved'
$MoveResult = Search-Mailbox @Params

Then just make sure the number of emails is equal. EXO can take a while to get synced up, so if it's not finding all the email to move back, just give it a while and run the second search again:

If ($TempResult.ResultItemsCount -eq $MoveResult.ResultItemsCount) {
  "We're good!"
} Else { 
  Write-Warning "Move results not equal!"
  $TempResult
  $MoveResult
}

Note that this is the 'Old' method, which is already retired and may be removed in the future. The new method is supposed to be using the New-ComplianceSearch style commands, but they have some limitations and aren't built for this purpose.

In my opinion, the "easiest" way is to open the mailbox in the browser > search for what you want > scroll down to load all the messages if needed > select all > move to > "move to a different folder..."

Upvotes: 1

Related Questions