Remove exchange contact internal smtp addresses

StaceD 20 Reputation points
2024-08-02T12:02:50.02+00:00

I am looking for a way to remove the internal email addresses created when creating a mail contact in Exchange 2019. I can go into each account in Active Directory and remove them but I would rather do it with PowerShell if possible. Specifically using the import-csv command. Does anyone know if that is possible?

Thanks

Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Exchange | Hybrid management
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-08-05T01:46:12.88+00:00

    Hi, StaceD

    Great to know that the issue has already been resolved and thanks for sharing the solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "[The question author cannot accept their own answer. They can only accept answers by others Accept answers on Microsoft Q&A | Microsoft Learn]", I'll repost your solution in case you'd like to "[Accept]" the answer : )     

    --------------   

    Issue Symptom: 

    Remove the internal email addresses created when creating a mail contact in Exchange 2019 

    Resolution: 

    $mailContacts = Import-Csv -Path "C:\XXXX\MailContactsToUpdate.csv"

    #Loop through each contact in the CSV

    foreach ($contact in $mailContacts){

    # Get the email address from the CSV
    $emailAddress = $contact.'Email Address'
    # Get the mail contact
    $mailContact = Get-MailContact -Identity $emailAddress -ErrorAction SilentlyContinue
    # Check if the mail contact exists
    if ($mailContact) {
        # Get the current email address (proxy addresses)
        $currentEmailAddresses = $mailContact.EmailAddresses
        # Remove only internal email addresses (proxy addresses starting with "SMTP:")
        $updatedEmailAddresses = $currentEmailAddresses | Where-Object { $_ -Clike 'SMTP:*' }
        # Allows the set-mailcontact command to run
        Get-Mailcontact -Identity $mailContact.Name | Set-Mailcontact -EmailAddressPolicyEnabled $false
        # Set the updated email addresses
        Set-MailContact -Identity $emailAddress -EmailAddresses $updatedEmailAddresses -ErrorAction Stop
        Write-Host "Updated: $emailAddress"
    } else {
        Write-Host "Mail contact not found: $emailAddress"
    }
    

    }

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. glebgreenspan 2,245 Reputation points
    2024-08-02T14:41:40.7566667+00:00

    Hello Stace

    Here is PowerShell script:

    Import the CSV file

    $mailContacts = Import-Csv -Path "C:\Path\To\MailContactsToUpdate.csv"

    Loop through each contact in the CSV

    foreach ($contact in $mailContacts) {

    # Get the email address from the CSV
    
    $emailAddress = $contact.EmailAddress
    
    # Get the mail contact
    
    $mailContact = Get-MailContact -Identity $emailAddress -ErrorAction SilentlyContinue
    
    # Check if the mail contact exists
    
    if ($mailContact) {
    
        # Get the current email address (proxy addresses)
    
        $currentEmailAddresses = $mailContact.EmailAddresses
    
        # Remove only internal email addresses (proxy addresses starting with "SMTP:")
    
        $updatedEmailAddresses = $currentEmailAddresses | Where-Object { $_ -notlike 'SMTP:*' }
    
        # Set the updated email addresses
    
        Set-MailContact -Identity $emailAddress -EmailAddresses $updatedEmailAddresses -ErrorAction Stop
    
        Write-Host "Updated: $emailAddress"
    
    } else {
    
        Write-Host "Mail contact not found: $emailAddress"
    
    }
    

    }


  2. StaceD 20 Reputation points
    2024-08-02T20:05:49.87+00:00

    Thank you both for your help. It didn't work exactly like I wanted so a co worker of mine made some changes to it and now it works perfectly!! You saved me a lot of time. I put the modified script below.

    $mailContacts = Import-Csv -Path "C:\XXXX\MailContactsToUpdate.csv"

    #Loop through each contact in the CSV

    foreach ($contact in $mailContacts){

    # Get the email address from the CSV
    
    $emailAddress = $contact.'Email Address'
    
    
    
    # Get the mail contact
    
    $mailContact = Get-MailContact -Identity $emailAddress -ErrorAction SilentlyContinue
    
    # Check if the mail contact exists
    
    if ($mailContact) {
    
        # Get the current email address (proxy addresses)
    
        $currentEmailAddresses = $mailContact.EmailAddresses
    
        # Remove only internal email addresses (proxy addresses starting with "SMTP:")
    
        $updatedEmailAddresses = $currentEmailAddresses | Where-Object { $_ -Clike 'SMTP:*' }
    
        # Allows the set-mailcontact command to run
    
        Get-Mailcontact -Identity $mailContact.Name | Set-Mailcontact -EmailAddressPolicyEnabled $false
    
        # Set the updated email addresses
    
        Set-MailContact -Identity $emailAddress -EmailAddresses $updatedEmailAddresses -ErrorAction Stop
    
        Write-Host "Updated: $emailAddress"
    
    } else {
    
        Write-Host "Mail contact not found: $emailAddress"
    
    }
    

    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.