PowerShell rename a mailbox folder using Exchange Web Services API


A question came up on the Exchange Server Forum on powershellcommunity.org:

‘Is it possible to use PowerShell with a COM access to connect to a remote outlook mailbox and change the name of a folder, especially a custom folder not a system folder like inbox,sent items,contact…etc? I have a project where I need to change the name of a folder called marketing that 1000 users have on their mailbox at the root of the mailbox and change it to something like Market-Values? If possible, can someone steer me into the right direction?”
After several probing replies, it was learned that the folder was really named “Marketing Deals”, and that the original requestor wouldn’t mind using the EWS Managed API, but that she was trying PowerShell “first”

Well, as Glen Scales keeps showing, PowerShell and EWS (especially the managed API) play together very well.

Using some of Glen’s previous work on finding a folder, and some of David Claux’s example on renaming a folder from the TechNet forums, I came up with this:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
$dllpath =`
“C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll”
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService(`
[Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = “LDAP://<SID=” + $windowsIdentity.user.Value.ToString() + “>”
$aceuser = [ADSI]$sidbind
$service.AutodiscoverUrl($aceuser.mail.ToString())
$Domain = $aceuser.mail.ToString().Split(‘@’)[1]
foreach ($MailboxName in Get-Content “c:\MailboxList.txt”)
{
$rfRootFolderID = new-object Microsoft.Exchange.WebServices.Data.FolderId(`
[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
$rfRootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$rfRootFolderID)
$fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(10000);
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$fvFolderView.PropertySet = $Propset
$ffResponse = $rfRootFolder.FindFolders($fvFolderView);
foreach ($folder in $ffResponse.Folders)
{
if ($folder.Displayname -eq “Marketing Deals”)
{
Write-Output “Found Marketing Deals on $mailboxName”
$folder.Displayname = “Market-Values”
$folder.Update()
}
}
}

If you are running Exchange 2007 SP1, changel line 5 from
[Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)
to
[Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)

You’d also obviously want to change the folder you are searching for, and the name you want to rename it to.

If you want to only rename a folder in the root of the mailbox, remove this line:

$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep

Finally, create a text file with email addresses (one per line), and save it as c:\mailboxlist.txt like:
karlmitschke@contoso.com
smitschke@contoso.com

Run the script, and any mailbox that has the searched for folder should have that folder renamed.

Advertisements
  1. #1 by Carl Nelson on October 20, 2010 - 17:12

    Thanks for this. This really helped me, and beats the heck out of using SOAP. The only part that may not work in all environments is:

    $service.AutodiscoverUrl($aceuser.mail.ToString())

    It complained about a malformed address in the mail method, so the solution to that was manually defining the service URL:

    $uri=[system.URI] ”https://whatever.company.com/ews/exchange.asmx”
    $service.Url = $uri

    Happiness ensued.

    • #2 by karlmitschke on October 20, 2010 - 19:12

      Thanks Carl;

      I did mean to put a disclaimer in abut the service.url 🙂

      You beat me to it.

      Karl

  2. #3 by mshicks on January 18, 2011 - 16:16

    If you could suffer my question I need some help with this…

    I try to run this but the folder I am trying to rename has a space in it, works on folders with no space. It throws this error:

    Exception calling “Update” with “0” argument(s): “The folder save operation failed due to invalid property values.”

    It finds the folder fine because it outputs the line.
    Write-Output “Found Marketing Deals on $mailboxName”

    I have tried using double quotes, single quotes, and escaping the space and I get the same error every time. Any ideas on how to get this to handle a space in the folder name? Is it possible?

    Thanks,

    Mike

    • #4 by Karl Mitschke on January 19, 2011 - 08:45

      Mike;

      I get no errors renaming folders with spaces.

      What are you trying to rename the folder TO?

      Karl

  3. #5 by mshicks on January 19, 2011 - 08:49

    Karl,

    Thanks for the response, I actually figured it out playing around with it this morning, the folder name I was using had an underscore in it and once I removed that it worked fine. I just didn’t have a chance to post my results here, but thanks for the quick response.

    Thank you,

    Mike

    • #6 by Karl Mitschke on January 19, 2011 - 09:05

      Mike;

      Glad you got it working.

      I just renamed “Market Values” to “New_Market_Values” on my mailbox.

      I am running Exchange 2010 SP1 though – Which version of Exchange are you running?

      Karl

  4. #7 by mshicks on January 20, 2011 - 11:24

    Karl,

    I got it working with the underscore not sure what was happening there. I am running 2010 SP1 as well. One more question, this works great for my mail box but when I try to run it against another mailbox it says it cant find the item in the store. I am assuming it is returning this because my user doesn’t have rights to the other users mailbox. What permission would be needed for this?

    Thanks,

    Mike

  5. #8 by mshicks on January 20, 2011 - 12:46

    Karl,

    Ignore my last comment I found a user that has the rights and logged in as that user and ran the script and it worked.

    Thanks for all the help,

    Mike

  6. #9 by Jim on August 6, 2011 - 15:50

    I ran the script and it runs with no errors but dosen’t change the name of my folder that i need changed. Is there log file or some way to see were its failing?

    • #10 by Karl Mitschke on August 8, 2011 - 10:51

      Jim;

      Does the script indicate that it found the folder?

      Karl

  7. #11 by Jim on August 9, 2011 - 07:12

    Karl

    I figured it out. I had a space in the folder. Removed it and works with no problems.

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: