- Get link
- X
- Other Apps
To Move Emails from One Folder to Another use the Below Scripts.
Note: This script is using Impersonation method, so you should use the account which has application impersonate rights.
Note: This script is using Impersonation method, so you should use the account which has application impersonate rights.
#Moving Emails from one folder to Another
#Author: Sunil Chauhan
#Email:sunilkms@gmail.com
#URL:Sunil-Chauhan.blogspot.com
#NOTE: This script is using Impersonation method, so you should use the account which has application impersonate rights.
#Change The Below parameters as your requirement
#no of items to move
$items=10
#Mailbox to move from
$mailbox=testMbx@myCompany.com
#Impersonate user Account
#if you wants to use the defaults credentials from the logged on user, you can use below options.
#$Service.UseDefaultCredentials = $true
$userName=admin@mycompany.com
$password=MyPasswrod
#folder details
$SourceFolder = "SourceFolder"
$destinationFolder="DestinationFolder"
#EWS url for your orgnization
$uri=[system.URI] "https://myCompany.com/ews/exchange.asmx"
#define EWS Dll file Path
$dllpath = "D:\sunil\Microsoft.Exchange.WebServices.dll"
#-------------------------------------------------------------------------------------
$pass = $password
$AccountWithImpersonationRights = $userName
$MailboxToImpersonate = $mailbox
Import-Module $dllpath
## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
## Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $AccountWithImpersonationRights, $pass
$service.url = $uri
Write-Host 'Using ' $AccountWithImpersonationRights ' Account to work in ' $MailboxToImpersonate
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress,$MailboxToImpersonate );
#Connect to the Inbox
$MailboxRootid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$ImpersonatedMailboxName)
$MailboxRoot=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$MailboxRootid)
#Getting Folders in the Mailbox
# you can change to folder view if there are more them 100 folder in the mailbox
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $MailboxRoot.FindFolders($FolderList)
#identifying Source Folder
$SourceFolderView=$findFolderResults | ? {$_.DisplayName -match $SourceFolder}
$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet `
([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::HTML
#Retrieving items from Source Folder
$SourceFolderItemView=New-Object Microsoft.Exchange.WebServices.Data.ItemView($items)
$SourceFolderItems=$service.FindItems($SourceFolderView.Id,$SourceFolderItemView)
[Void]$service.LoadPropertiesForItems($SourceFolderItems,$psPropset)
#Identifying destination Folder
$trgFolder=$findFolderResults | ? {$_.DisplayName -match $destinationFolder}
# Now Moving All Items from Source Folder to Target Folder.
foreach ($EmailItem in $SourceFolderItems)
{
$EmailItem.move($trgFolder.id) | Out-null ;
write-host "Moved Item:" $EmailItem.subject
}
- Get link
- X
- Other Apps
Comments
Post a Comment