Stripping/Removing a Character - Email Address (User and Distribution Groups)

Hello experts,
I have been trying to test and no dice with this script (provided by another expert in another question that I posted). I wanted to strip away/remove "$" and "!" from the email addresses (as a prerequisite for ADAzure directory sync), and for some reason it just does not work. Can someone please help and provide some light?

$mailboxes = Get-Mailbox -ResultSize Unlimited | Select-Object Alias,ExchangeGUID,EmailAddresses
foreach ($mailbox in $mailboxes) {

$aliases = $mailbox | select -ExpandProperty EmailAddresses
$aliases -replace "\$|\!",""
Set-Mailbox $mailbox.ExchangeGuid.ToString() -EmailAddresses $aliases
}
IT_Admin XXXXAsked:
Who is Participating?
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.

oBdACommented:
Because you're writing back the exact same value you read, because you never re-assign the $aliases value you're writing back ...
$mailboxes = Get-Mailbox -ResultSize Unlimited | Select-Object Alias,ExchangeGUID,EmailAddresses
foreach ($mailbox in $mailboxes) {
	$aliases = $mailbox | select -ExpandProperty EmailAddresses
	$aliases = $aliases -replace '\$|\!', ''
	Set-Mailbox $mailbox.ExchangeGuid.ToString() -EmailAddresses $aliases
}

Open in new window

IT_Admin XXXXAuthor Commented:
@oBdA - Thank you for the quick response! The script seems to execute successfully (with errors that are expected due to some mailboxes that are corrupted), but the characters are still in there (not removed).

I queried using the following to check and they are still there :(

Get-Recipient -ResultSize Unlimited | where {$_.emailaddresses -like "*!*"} | select name,emailaddresses
Vasil Michev (MVP)Commented:
Well, just run it against a single one of the affected mailboxes, and make sure you note the value of $aliaes before and after the changes. Perhaps it's not parsing the EmailAddresses correctly, as there are slight differences depending on whether you run the code in the EMS or remote PS.

$mailboxes = Get-Mailbox some_affected_mailbox | Select-Object Alias,ExchangeGUID,EmailAddresses
foreach ($mailbox in $mailboxes) {
	$aliases = $mailbox | select -ExpandProperty EmailAddresses
	$aliases
	$aliases = $aliases -replace '\$|\!', ''
	$aliases
	Set-Mailbox $mailbox.ExchangeGuid.ToString() -EmailAddresses $aliases
} 

Open in new window


Also, make sure the mailbox is not subject to an email address policy (Set -EmailAddressesPolicyEnabled to false)
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

IT_Admin XXXXAuthor Commented:
@Vasil :) thank you for your help always. I ran the script against (1) affected mailbox, added -emailaddresspolicyenabled $false in the set-mailbox line and still no dice :(
oBdACommented:
Well, what's the output of the aliases that's generated before and after the the replace?
Is the "!" still there in the second line?
IT_Admin XXXXAuthor Commented:
The aliases of the test impacted mailbox is:
!useraccount@domain.com
$useraccount@domain.com
useraccount@domain.com

after is the same as the above
oBdACommented:
Could it be that "after" is actually
useraccount@domain.com
useraccount@domain.com
useraccount@domain.com
That would be why then - you're trying to set the same alias three times, so it's not letting you.
Try it like this:
$mailboxes = Get-Mailbox -ResultSize Unlimited | Select-Object Alias,ExchangeGUID,EmailAddresses
foreach ($mailbox in $mailboxes) {
	$aliases = $mailbox | select -ExpandProperty EmailAddresses
	$aliases = $aliases -replace '\$|!', '' | Select-Object -Unique
	Set-Mailbox $mailbox.ExchangeGuid.ToString() -EmailAddresses $aliases
}

Open in new window

Vasil Michev (MVP)Commented:
Just to avoid another corner case - you should probably add a check for the scenario when primary and secondary SMTP addresses match, post removal of those characters.
IT_Admin XXXXAuthor Commented:
@oBDA - It worked! I used double quotes "" vs single quotes btw. Thank you!

@Vasil - Thank you for the information. I followed your blog (which btw solves my cleanup of all .local extension other issue) and the fact that it covers the primary vs. secondary smtp, it is genius. I tried running the following (and added some not to delete x400 and x500 addresses as highlighted because without it, it deletes those addresses), AND rather than 'stripping' the character, it deleted the whole mailbox that has "$". Please let me know what i am doing wrong. This is good to know as good code to have for cleanup reasons! THANKS!

$mailboxes = Get-Mailbox -ResultSize Unlimited | Select-Object Alias,ExchangeGuid,Emailaddresses
foreach ($mailbox in $mailboxes) {
 
    $aliases = $mailbox | select -ExpandProperty EmailAddresses |?{$_.prefixstring -ne "x400" -or $_.prefixstring -ne "x500"} |  ? {$_.Split(":")[1] -notlike '*$*'}
    if ($aliases.Count -eq $mailbox.EmailAddresses.Count) {continue}
 
    if ($aliases | ? {$_ -cmatch "SMTP:"}) {
        $emailaddresses = $aliases
    }
    elseif (!$aliases) { $emailaddresses = $("SMTP:" + $mailbox.Alias + "@" + (Get-AcceptedDomain | ? {$_.Default -eq $true}).Name)  }
    else {
        $emailaddresses = ($aliases -join "," -replace "smtp:(?!.*smtp:)","SMTP:") -split ","
    }
    Set-Mailbox $mailbox.ExchangeGuid.ToString() -EmailAddresses $emailaddresses
}
Vasil Michev (MVP)Commented:
What do you mean deleted the whole mailbox?! At worst, the cmdlet above could mess up the mailbox's email aliases, nothing more. I'd suggest you check the admin audit log to find out what deleted the mailbox, it's definitely not the code above.
IT_Admin XXXXAuthor Commented:
@Vasil - apologies wrong use of word. I meant it deleted the alias (not the mailbox). The intent is to just strip out/away the character but it deleted the alias containing the character. Hope that makes sense.
Vasil Michev (MVP)Commented:
Ah, yes, that's what the sample does - it looks into the list of aliases and filters out any offenders. It was made for a different scenario, if you want to replace them instead of filtering them out simply use the other sample.

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
IT_Admin XXXXAuthor Commented:
Thank you for all your help!
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
Powershell

From novice to tech pro — start learning today.