locked
Powershell Exchange 2010 Delete Folder in Mailbox / Or Delete Mail In Certain Folder. RRS feed

  • Question

  • I can't seem to work out how to delete a certain folder in a mailbox. I'm importing a PST to a mailbox and ideally would be to use the -DeleteContent switch, but that cmdlet isn't available in Exchange 2010 SP1 without installing the 64bit client. Installing a 64bit Outlook Client isn't an option.

    How can I delete a certain folder in a mailbox? It's in the root of the user mailbox (so not in the inbox).
    Deleting all mail in that folder would also be a solution.

    Wednesday, September 28, 2011 9:00 AM

Answers

  • I've done it with EWS:


    $MailboxName = 'Name@Mailbox'

    $dllpath = "D:\EWS\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.AutodiscoverUrl($MailboxName,{$true})

    $RootFolderID = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
    $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$RootFolderID)

    $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep

    $Response = $RootFolder.FindFolders($FolderView)

    ForEach ($Folder in $Response.Folders) {
      if($folder.DisplayName -eq "AName") {
        $folder.delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete) } }

    Wednesday, September 28, 2011 2:44 PM
  • You can use the Exchange Web Services Managed API with Powershell to delete items and/or folders in an Exchange mailbox.

    http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=13480

    One of my favorite sources for examples of using the API with Powershell is Glen Scales blog:

    http://gsexdev.blogspot.com/


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by Squ1zZy Wednesday, September 28, 2011 2:43 PM
    Wednesday, September 28, 2011 10:26 AM

All replies

  • You can use the Exchange Web Services Managed API with Powershell to delete items and/or folders in an Exchange mailbox.

    http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=13480

    One of my favorite sources for examples of using the API with Powershell is Glen Scales blog:

    http://gsexdev.blogspot.com/


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by Squ1zZy Wednesday, September 28, 2011 2:43 PM
    Wednesday, September 28, 2011 10:26 AM
  • I've done it with EWS:


    $MailboxName = 'Name@Mailbox'

    $dllpath = "D:\EWS\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.AutodiscoverUrl($MailboxName,{$true})

    $RootFolderID = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
    $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$RootFolderID)

    $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep

    $Response = $RootFolder.FindFolders($FolderView)

    ForEach ($Folder in $Response.Folders) {
      if($folder.DisplayName -eq "AName") {
        $folder.delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete) } }

    Wednesday, September 28, 2011 2:44 PM
  • Amazing Script. I like it.

    Cheers,
    Thomas


    Thomas Stensitzki - MCPD, MCITP, MCTS, MCSA, MCSA:Messaging
    Thursday, November 24, 2011 2:55 PM
  • Thank you for the script.  I was able to use this on one username at a time.  How can I do this for a group of mailboxes in our organization?  Any assistance is appreciated.  Thanks.

    Tuesday, March 27, 2012 4:32 PM
  • Hi

    It doesn't work. I have Ex 2010 SP2 and downloaded "Exchange Web Services Managed API"

    Errors:

    Exception calling "Bind" with "2" argument(s): "Connection did not succeed. Try again later."
    At C:\Administracja\Skrypty\T-MailboxFolderDelete.ps1:15 char:65
    + $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind <<<< ($Service,$RootFolderID)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    You cannot call a method on a null-valued expression.
    At C:\Administracja\Skrypty\T-MailboxFolderDelete.ps1:20 char:36
    + $Response = $RootFolder.FindFolders <<<< ($FolderView)
        + CategoryInfo          : InvalidOperation: (FindFolders:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    So what is wrong in your opinion?

    Thank you

    Tomasz.


    Kind Regards Tomasz

    Friday, March 30, 2012 9:47 AM
  • Tomasz, I'm having the same issue also.  Anyone have any ideas?
    Wednesday, December 12, 2012 5:40 PM
  • you must have the accessright {FullAccess } to issue target mailbox
    Friday, February 8, 2013 10:24 AM
  • you must have the accessright {FullAccess } to issue target mailbox
    • Proposed as answer by Yukio Seki Tuesday, February 12, 2013 2:56 PM
    Friday, February 8, 2013 10:24 AM
  • Any Idea If you have this error:

    "Bind" with "2" argument(s): "The mailbox database is temporarily unavailable." "

    Is like the database is not there but it is. Any specific name that I have to use? I am running exchage 2010 SP3


    gherrera


    Friday, August 9, 2013 4:15 PM
  • Hi! 

    Is there a method to distinguish between "user created folders" vs "mailbox standard folders" in EWS? Are there any properties that marks a folder as "system created"?

    I want to be able to run scripts targeting user created folders without the need to know the name of the folder, and also be certain that system folders are not affected.

    Best Regards,

    Magnus

    Wednesday, August 21, 2013 11:26 AM
  • Hi,

    I have tested the script and it works just perfect.

    I need to run it against a lot of users - I have them all listed in a .csv file.

    So I have tried adding the following in the top

    $users = Import-CSV C:\scripts\users.csv -delimiter ';'
    $users | foreach-object {

    And then changed to $MailboxName = $_.Email 

    And it doesn't work - get a lot of errors.

    As you might have guessed my powershell skills are very poor :)

    Tuesday, October 29, 2013 1:39 PM
  • This thread is old and marked as answered, please start a new question and post your code/errors there.

    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,225+ strong and growing)

    Tuesday, October 29, 2013 1:45 PM