Error with string processing

I’m trying to create and run a command in powershell.

I have a list of email addresses to remove and when I try to create a command it is not working.


get-mailbox -resultsize unlimited | search mailbox -searchquery from:"" -targetmailbox Cleanup -targetfolder Processed -deletecontent -loglevel full

In PS I cannot combine the strings

$CMDString = “get-mailbox -resultsize unlimited | search mailbox -searchquery from:” +“”" + $emailaddress + “”" +"

-targetmailbox Cleanup -targetfolder Processed -deletecontent -loglevel full"

This is the end result:

" -targetmailbox Cleanup -targetfolder Processed -deletecontent -loglevel full

It appears anything after the variable overwrites the beginning of the string.


Hi coulbc59

I am not an expert in Exchange Server though, but for your requirement to manage with the strings in the arguments you can run the command like below…

[pre]Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery 'from:"$emailaddress"' -TargetMailbox Cleanup -TargetFolder Processed -DeleteContent -LogLevel Full[/pre]
And please format the code using PRE tags to highlight the code.
Thank you.

This is not an Exchange issue. I’m trying to build a command line through a string and then invoke it. The problem is the string is not parsing correctly.


Get-Mailbox -resultsize unlimited | Search-Mailbox -SearchQuery from:"" -TargetMailbox MessageCleaning -TargetFolder Processed -DeleteContent -LogLevel Full


What it tries to invoke.

" -TargetMailbox MessageCleaning -TargetFolder Processed -DeleteContent -LogLevel –


When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

This might help you toi understand:

$emailAddress = ''
Get-Mailbox -ResultSize Unlimited | 
    Search-Mailbox -SearchQuery "from:$emailAddress" -TargetMailbox MessageCleaning -TargetFolder Processed -DeleteContent -LogLevel Full

You have to provide a filter string for the parameter -SearchQuery. So the “from” has to be included in the filter string. :wink: