Powershell script to delete secondary SMTP addresses of Exchange 2010 Mail Contacts

exchangeexchange-2010powershell

I have a few thousand Exchange 2010 Mail Contacts who get erroneously assigned internal SMTP addresses by the default recipient policy.

I'm trying to use the following command to delete these addresses (keeping the primary SMTP) and disabling the automatic update from recipient policy so the SMTP addresses don't get recreated again.

Get-MailContact -OrganizationalUnit "domain.local/OU" -Filter {EmailAddresses -like *@domain.local -and name -notlike "ExchangeUM*"} -ResultSize unlimited -IgnoreDefaultScope | foreach {
    $contact = $_
    $email = $contact.emailaddresses
    $email | foreach {
        if ($_.smtpaddress -like *@domain.local) {
            $address = $_.smtpaddress
            write-host "Removing address" $address "from Contact" $contact.name
            Set-Mailcontact -Identity $contact.identity -EmailAddresses @{Remove=$address}
            $contact | set-mailcontact -emailaddresspolicyenabled $false
        } 
    }
}

I'm getting the following error though:

You must provide a value expression on the right-hand side of the '-like' operator.
At line:1 char:312
+ Get-MailContact -OrganizationalUnit "domain.local/testou" -Filter {EmailAddresses -like "*@domain.local" -and name -notlike "ExchangeUM*"} -ResultSize unlimited -IgnoreDefaultScope | foreach {$contact = $_; $
email = $contact.emailaddresses; $email | foreach {if ($_.smtpaddress -like <<<<  *@domain.local) {$address = $_.smt
paddress; write-host "Removing address" $address "from Contact" $contact.name; Set-Mailcontact -Identity $contact.ident
ity -EmailAddresses @{Remove=$address}; $contact }}
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

Any help as to how to fix this?

Best Answer

You need quotes around *@domain.local