Hello there,
To move emails from one folder to another folder for all mailboxes in an Exchange environment, you can use PowerShell cmdlets provided by Exchange Management Shell (EMS). Here's a script that demonstrates how to achieve this task:
powershell
Copy code
# Connect to Exchange Online or Exchange Server (adjust the connection cmdlet based on your environment)
# For Exchange Online:
# Import-Module ExchangeOnlineManagement
# Connect-ExchangeOnline -Credential $Cred
# For Exchange Server (on-premises):
# Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
# Replace <SourceFolder> and <TargetFolder> with the names of the source and target folders
$SourceFolder = "<SourceFolder>"
$TargetFolder = "<TargetFolder>"
# Get all mailboxes in the organization
$mailboxes = Get-Mailbox -ResultSize Unlimited
# Loop through each mailbox
foreach ($mailbox in $mailboxes) {
$mailboxName = $mailbox.PrimarySmtpAddress
# Connect to the mailbox using PowerShell's implicit remoting
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://<ExchangeServerFQDN>/PowerShell/" -Authentication Kerberos -Credential $Cred
Import-PSSession $session
try {
# Get the source folder and target folder in the mailbox
$sourceFolderId = (Get-MailboxFolderStatistics -Identity $mailboxName).Where({ $_.Name -eq $SourceFolder }).FolderId
$targetFolderId = (Get-MailboxFolderStatistics -Identity $mailboxName).Where({ $_.Name -eq $TargetFolder }).FolderId
# Move emails from the source folder to the target folder
Search-Mailbox -Identity $mailboxName -SearchQuery "kind:email AND folder:$SourceFolder" -TargetMailbox $mailboxName -TargetFolder $TargetFolder -LogLevel Full -DeleteContent -Force
Write-Host "Moved emails from $SourceFolder to $TargetFolder for mailbox $mailboxName."
} catch {
Write-Host "Error moving emails for mailbox $mailboxName: $_"
} finally {
# Remove the PowerShell session
Remove-PSSession $session
}
}
Before running the script, make sure to perform the following steps:
Uncomment the appropriate connection cmdlets (either for Exchange Online or Exchange Server) and adjust them according to your environment.
Replace <SourceFolder> and <TargetFolder> with the actual names of the source and target folders.
Ensure you have the necessary permissions to access and move emails in all mailboxes.
I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.
Hope this resolves your Query !!
--If the reply is helpful, please Upvote and Accept it as an answer–
Move mails from one folder to another folder via powershell script for all mailboxes
Hello,
Recently, I have done a cross tenant mailbox migration by manually export and import pst procedure. Now, I have been facing an issue. My all mails were imported in outlook on the web under TargetRootFolder which created by System. Now, I want to move mails to my default folder. For example, source pst inbox mails are in TargetRootFolder->Inbox, source pst deleted items mails are in TargetRootFolder -> Deleted Items. Now I want to move all argetRootFolder->Inbox folder's mail to my default Inbox folder TargetRootFolder -> Deleted Items mails to my default Deleted Items folder. Now, how can I do this for all mailboxes by a powershell script?
Thank you
3 answers
Sort by: Most helpful
-
Deleted
This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.
1 deleted comment
Comments have been turned off. Learn more
-
Limitless Technology 44,381 Reputation points
2023-07-31T10:32:36.8633333+00:00 -
35874177 0 Reputation points
2024-06-13T20:32:50.0533333+00:00 Search-Mailbox is depreciated