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

IT_Admin XXXX
IT_Admin XXXX used Ask the Experts™
on
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
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

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
Most Valuable Expert 2015
Distinguished Expert 2018

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)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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 :(
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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?

Author

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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Most Valuable Expert 2015
Distinguished Expert 2018

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.

Author

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
}
Most Valuable Expert 2015
Distinguished Expert 2018

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.

Author

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.
Most Valuable Expert 2015
Distinguished Expert 2018
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.

Author

Commented:
Thank you for all your help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial