none
How to move outlook subfolders RRS feed

  • Question

  • Hello,

    Is it possible to move subfolders from one folder to another within one mailbox? I need to move folders from

    MsgFolderRoot\OldMail to MsgFolderRoot\Managed Folders

    For example folder

    MsgFolderRoot\OldMail\Received will go to  MsgFolderRoot\Managed Folders\Received  

    MsgFolderRoot\OldMail\Sent will go to  MsgFolderRoot\Managed Folders\Sent

    etc.  

    How to implement it with PowerShell and EWS API? (Exchange server 2010)

    Thank you 

    Friday, July 1, 2011 3:19 AM

Answers

  • I just did similar thing yesterday:
    $MailboxName = vojin.lekovic@contoso.lab
    $uri=[system.URI] "https://mail.contoso.lab/ews/exchange.asmx"
    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
    [void][Reflection.Assembly]::LoadFile($dllpath)
    $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
    $service.Url = $uri
    $service.Credentials = New-Object System.Net.NetworkCredential("user","password","domain")
    Function FindFolder($FolderPath){
    $resFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot)
    $pfArray = $FolderPath.Split("/")
    for ($lint = 1; $lint -lt $pfArray.Length; $lint++) {
    $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)
    $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$pfArray[$lint])
    $findFolderResults = $service.FindFolders($resFolder.Id,$SfSearchFilter,$fvFolderView)
    if ($findFolderResults.TotalCount -gt 0){
    foreach($folder in $findFolderResults.Folders){
    $resFolder = $folder
    }}
    else{"Error Folder Not Found"
    $resFolder = $null
    break
    } }
    return $resFolder
    }
    $fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(10000)
    $fvItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(10000)
    $folders = @()
    $folders += "" | select @{n='SourceFolder';e={'/Retained e-mails/(a) 90 days'}}, @{n='DestinationFolder';e={'/Managed Folders/(a) 90 days'}}
    $folders += "" | select @{n='SourceFolder';e={'/Retained e-mails/(b) 6 months'}}, @{n='DestinationFolder';e={'/Managed Folders/(b) 6 months'}}
    $folders += "" | select @{n='SourceFolder';e={'/Retained e-mails/(c) 1 year'}}, @{n='DestinationFolder';e={'/Managed Folders/(c) 1 year'}}
    $folders += "" | select @{n='SourceFolder';e={'/Retained e-mails/(d) 2 years'}}, @{n='DestinationFolder';e={'/Managed Folders/(d) 2 years'}}
    foreach ($folderPair in $folders)
    {
    $folderPair
    $sourceFolder = FindFolder($folderPair.SourceFolder)
    $destinationFolder = FindFolder($folderPair.DestinationFolder)
    if ($sourceFolder -ne "Error Folder Not Found")
    {#move items
    foreach($item in $sourceFolder.FindItems($fvItemView))
    {$item.Move($destinationFolder.Id) | Out-Null
    "Movin item: $($item.Subject)"}
    #Move subfolders
    foreach($folder in $sourceFolder.FindFolders($fvFolderView)){
    $Folder.Move($destinationFolder.Id) | Out-Null
    "Moving folder: $($folder.DisplayName)"}
    #Delete source folder
    $sourceFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::MoveToDeletedItems)
    }}

    • Proposed as answer by Vojin Lekovic Wednesday, July 6, 2011 6:09 AM
    • Marked as answer by WictorL Thursday, July 14, 2011 5:14 PM
    Tuesday, July 5, 2011 6:29 PM