Remove E-mails From Public Folders

Hello Experts.  
To remove an e-mail from all mailboxes I use this command.

Get-Mailbox | Search-Mailbox -SearchQuery "From:'XXXX'" -LogOnly -TargetMailbox administrator  -TargetFolder Inbox
and
Get-Mailbox | Search-Mailbox -SearchQuery "From:'XXXX'" -DeleteContent.

This does not hit my Public Folders.   I am trying to write a basic command to also remove e-mails from Public Mail Enabled Folders.  

Get-PublicFolder | Search-Mailbox -SearchQuery "From:'XXX'" -LogOnly -TargetMailbox administrator -TargetFolder \research

Cannot process argument transformation on parameter 'Identity'. Cannot convert the "\" value of type "Microsoft.Exchange.Data.Mapi.PublicFolder Id" to type "Microsoft.Exchange.Configuration.Tasks.MailboxOrMailUserIdParameter".
    + CategoryInfo          : InvalidData: (Microsoft.Excha...pi.PublicFolder:PSObject) [Search-Mailbox], ParameterBin    din...mationException     + FullyQualifiedErrorId : ParameterArgumentTransformationError,Search-Mailbox

Any Suggestions ??

Thank you in advance for your assistance.
cnetwizAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Satyendra SharmaMicrosoft UC Technical ArchitectCommented:
cnetwizAuthor Commented:
Hello,
I already reviewed the first site.  
The purpose of this script is not to move but to delete a mail item from ALL public folders identified by similiar Subject or From as my example below shows:
Get-PublicFolder | Search-Mailbox -SearchQuery "From:'xxxx'" -LogOnly -TargetMailbox  administrator -TargetFolder "\Public Folders\All Public Folders\"

I can successfully run a smiliar script on the Exchange Mailboxes and it works perfectly.
Get-Mailbox | Search-Mailbox -SearchQuery "From:'XXXX'" -LogOnly -TargetMailbox administrator  -TargetFolder Inbox

When I run a Public Folder item,  I keep getting errors.  

I am trying to do the same thing but to the Public Folders.

Cannot process argument transformation on parameter 'Identity'. Cannot convert the "\" value of type "Microsoft.Exchange.Data.Mapi.PublicFolder Id" to type "Microsoft.Exchange.Configuration.Tasks.MailboxOrMailUserIdParameter".
     + CategoryInfo          : InvalidData: (Microsoft.Excha...pi.PublicFolder:PSObject) [Search-Mailbox], ParameterBin    din...mationException     + FullyQualifiedErrorId : ParameterArgumentTransformationError,Search-Mailbox
SteveCommented:
you cannot just specify a top level public folder (ie "\") as exchange has no way to translate that to the entire folder structure.

try getting the command to work on a specific folder first to check the command will actually work as you want it to.

Then have a look at the Microsoft provided script that adds permissions to public folders:
AddUsersToPFRecursive.ps1 (should be in your scripts folder)

This Powershell script actually gets a full public folder list and then applies the required permission to each folder one at a time.
To achieve what you need you'll have to do the same by recursively searching each folder. Use AddUsersToPFRecursive.ps1 as a starting point.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rajitha ChimmaniCommented:
You are getting this error as Search-Mailbox command works only against a mailbox and hence it does not recognize "\" as a valid object.

As I know, there is no direct command to delete items from a public folder using powershell. I have not seen one yet but I guess its possible through VB
cnetwizAuthor Commented:
I have discovered that deleting items from All Public Folders is not possible unless it is with a VB Script.   With the many Public Folders existing in our organizaiton, I will open a Pay Ticket with Microsoft to see what tricks they have up their sleeves.   I will gladly post any suggestions and resolutions from Microsoft here.     Thank you .
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.