Powershell - Cleanup proxyAddresses/SMTP Attribute with Invalid/Special Characters

Hello PS experts,
Would like to ask for some help I tried running this PS script to update SMTP addresses/proxyAddresses field by removing the invalid characters "$" and/or "!"  and the script executes with no errors but the email addresses doesn't get updated.

$mailboxes = Get-Mailbox  | Select-Object Alias,ExchangeGuid,Emailaddresses
foreach ($mailbox in $mailboxes) {

$aliases = $mailbox | select -ExpandProperty EmailAddresses
$aliases -replace "\$|\!",""
Set-Mailbox $mailbox.ExchangeGuid.ToString() -EmailAddresses $aliases
}


Please help! Thank you.
IT_Admin XXXXAsked:
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.

Jason CrawfordTransport NinjaCommented:
This should do the trick:

foreach ($user in (Get-Mailbox)) {
  $alias = $user.emailaddresses -replace "\$|\!",""
  Set-Mailbox $user.ExchangeGuid.ToString() -EmailAddresses @{replace=$alias}
}

Open in new window

0
IT_Admin XXXXAuthor Commented:
@Jason thank you for the quick reply. If I wanted to test this against one user (to make sure I don't break things even in non-prod), should I do it this this way?

foreach ($user in (Get-Mailbox -identity "my alias")) {
  $alias = $user.emailaddresses -replace "\$|\!",""
  Set-Mailbox $user.ExchangeGuid.ToString() -EmailAddresses @{replace=$alias}
}
0
Jason CrawfordTransport NinjaCommented:
That would work or this:

foreach ($user in (Get-Mailbox | Select-Object -First 1)) {
  $alias = $user.emailaddresses -replace "\$|\!",""
  Set-Mailbox $user.ExchangeGuid.ToString() -EmailAddresses @{add=$alias}
}

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Shaun VermaakTechnical SpecialistCommented:
foreach ($user in (Get-Mailbox))
{
    $alias = $user.emailaddresses -replace "\$|\!","";
    if ($alias -ne $user.emailaddresses)
    {
        Set-Mailbox $user.ExchangeGuid.ToString() -EmailAddresses @{replace=$alias};
    }
}

Open in new window

0

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:
@Jason Crawford - thanks again..hmmm, it executed successfully but the item/object that was referenced in the message didn't get updated ("!" is still in the user's SMTP email address)....

@Shaun Vermaak - thank you..! question, how can I test this on a single mailbox *(e, "mymailbox")?
0
Shaun VermaakTechnical SpecialistCommented:
Same, just add -First 1 etc.
0
IT_Admin XXXXAuthor Commented:
@ Shaun - added - First 1..etc and got the following:

Cannot process argument transformation on parameter 'EmailAddresses'. Cannot convert value
"System.Collections.Hashtable" to type "Microsoft.Exchange.Data.ProxyAddressCollection". Error:
"Unknown operation: 'replace'. The supported keys to add values are: 'Add', '+'. The supported
keys to remove values are: 'Remove', '-'."
    + CategoryInfo          : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
    + PSComputerName        : server.domain.com
0
Jason CrawfordTransport NinjaCommented:
How would you feel if the script appended to the list of addresses instead of removing the ones containing the special characters?
0
IT_Admin XXXXAuthor Commented:
@Jason Crawford - thanks for the quick response. It should be ok. I actually have a list .csv in this format that I generated from the IDF tool.  ->


DISTINGUISHEDNAME      OBJECTCLASS      ATTRIBUTE
0
Jason CrawfordTransport NinjaCommented:
Ok I think this should at least append to the list:

foreach ($user in (Get-Mailbox)) {
  $alias = $user.emailaddresses -replace "\$|\!",""
  Set-Mailbox $user.ExchangeGuid.ToString() -EmailAddresses @{add=$alias}
}

Open in new window

0
IT_Admin XXXXAuthor Commented:
@ Jason Crawford - Thanks so much for looking into this for me. sorry for the confusion. The list I have is a query we have pulled from this tool called IDFix. Our main goal is to remove these illegal characters/special characters from the proxyAddresses (aka email fields) so we can perform a directory sync for Azure AD/O365.... The script above and yours execute successfully but for some reason it does not update the mailboxes
0
IT_Admin XXXXAuthor Commented:
Would this work instead?

Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase 'OU=Test,DC=domain,DC=local' -Properties proxyaddresses | Foreach {Set-ADUser -identity $_ -Replace @{'ProxyAddresses' = @($_.proxyaddresses -Replace "\$|\!","")}}
0
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.