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?
 
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
Live Q & A: Securing Your Wi-Fi for Summer Travel

Traveling this summer? Join us on June 18, 2018 for a live stream to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
Shaun VermaakTechnical Specialist/DeveloperCommented:
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
 
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 Specialist/DeveloperCommented:
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
All Courses

From novice to tech pro — start learning today.