We help IT Professionals succeed at work.

Powershell script to remove folder directory and contents from mailbox

Carl Billington
Carl Billington used Ask the Experts™
on
Hi,

Is there a script I can run in Powershell that will delete all folders with the name "ODIR_Duplicate_Items" in a mailbox.

This folder is listed multiple times throughout the mailbox and I would love to run and script that would delete all folders with this name including their contents.

Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Victor TarasovHead of system administration department

Commented:
read this

or
This command pipe will list the folders with spam in the name
get-mailboxfolderstatistics -identity <DisplayName> | foreach ( $_ ) {if
($_.Name -match "Spam") { Write-host $_.Name }}

Open in new window

Author

Commented:
Hi, I don't want to list the folders I want to delete them. Can you assist?
Victor TarasovHead of system administration department

Commented:
You can use Redemption Data Objects (RDO) com object to iterate all users
mailboxes and delete the spam folder.
RDO is a CDO replacment library. You can download a license free developer
version here:
http://www.dimastr.com/redemption/Redemption.zip

More information on RDO:
http://www.dimastr.com/redemption/rdo/default.htm


This will delete the spam folder and all of its subfolders when the folder
exist under the root store:

$rdo = new-object -com Redemption.RDOSession
$rdo.LogonExchangeMailbox("<userEmailAddress>","<ExchangeServerName>")
#$rdo.CurrentUser.name

$spam = $rdo.Stores.DefaultStore.IPMRootFolder.folders | where { $_.name
-eq "spam"}
if($spam) {$spam.delete()}

$rdo.logoff()

Author

Commented:
Hi, thanks for this!

What do you mean when you say "when the folder exist under the root store?". The folder exists multiple times within many subdirectories throughout the mailbox. I need something that will search through all folders listed.

Let me know, ta

Author

Commented:
Hi VictorTarasov anymore comments on this?
I deleted the folders manually.

Author

Commented:
Deleted manually.