none
How find and replace email domain text for AD users?

    Question

  • There was a company name change for some of our external AD users.

    They have their email addresses in 2 possible user account fields.  "Office" and "E-mail."

    How can I do a find and replace the text @oldcompany.com with @newcompany.com when found in either the Office or E-Mail field or both?

    Wednesday, April 26, 2017 12:33 AM

Answers

  • Use this code. I wrote it for you.

    Import-Module activedirectory
    Clear-Host
    $Users = Get-ADUser -Filter {(Mail -like "*@oldcompany.com") -or (physicalDeliveryOfficeName -like "*@oldcompany.com")} -Properties physicalDeliveryOfficeName,mail
    foreach($user in $Users)
    {
        try
        {  
            if ($user.mail)
            { 
                $OldMail   = $user.mail
                $NewMail = $OldMail.Replace("@oldcompany.com","@NewCompany.com")
                Set-ADUser $user -Replace @{"mail"=$NewMail} 
            }
    
            else
            {
                $OldOffice = $user.physicalDeliveryOfficeName
                $NewOffice = $OldOffice.Replace("@oldcompany.com","@NewCompany.com")
                Set-ADUser $user -Replace @{"physicalDeliveryOfficeName"=$NewOffice} 
            }
        }
        catch
        {
            Write-Warning "$_"
        }
    }


    Mahdi Tehrani | | www.mahditehrani.ir
    Make sure to download my free PowerShell scripts:

    • Proposed as answer by AlvwanModerator Tuesday, May 2, 2017 1:16 AM
    • Marked as answer by Kalimanne Tuesday, May 2, 2017 3:03 AM
    Wednesday, April 26, 2017 2:18 PM
    Moderator

All replies

  • Hi,

    To my knowledge, we could use Active Directory Domains and Trusts to add user principal name (UPN) suffixes for the existing user account. The default UPN suffix for a user account is the Domain Name System (DNS) domain name of the domain that contains the user account. You can add alternative UPN suffixes to simplify administration and user logon processes by providing a single UPN suffix for all users. The UPN suffix is used only within the Active Directory forest, and it is not required to be a valid DNS domain name.

    HOW TO: Add UPN Suffixes to a Forest

    http://support.microsoft.com/kb/243629

    Add User Principal Name Suffixes

    https://technet.microsoft.com/en-us/library/cc772007(v=ws.11).aspx

    Then on the user account change this drop down.

    Best Regards,

    Alvin Wang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, April 26, 2017 2:43 AM
    Moderator
  • I don't think you understood the question.

    We are not changing our local domain.

    We just need to edit the text in the specified fields for some external users that have accounts on our domain.

    We need to find all the ones with the old domain name and edit the text in only those accounts in some kind of automated way.

    Wednesday, April 26, 2017 2:58 AM
  • Hi,

    Adding UPN suffix will not change your domain in actual. 

    From your description, you want to replace the text @oldcompany.com with @newcompany.com without domain rename, then using UPN suffix should be a good choice.

    How Domain Rename Works

    https://technet.microsoft.com/en-us/library/cc738208(v=ws.10).aspx  

    Best Regards,

    Alvin Wang



    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, April 26, 2017 3:41 AM
    Moderator
  • We do not want to change the domain UPN for their user accounts.

    We only want to change part of the text typed into the "Office" field and the "E-Mail" field in their account properties. Whenever a user is found with @oldcompant.com in the "Office" or "E-Mail" field, we want to change that text to @newcompany.com.

    Their login ID is not changing.  They will still use our current domain name for their account login. 

    Wednesday, April 26, 2017 3:47 AM
  • You need to proceed as the following:

    1. Add the UPN suffix in your forest: https://technet.microsoft.com/en-us/library/cc772007%28v=ws.11%29.aspx?f=255&MSPPError=-2147217396
    2. Create a Powershell script and use it for mass updates: https://blogs.technet.microsoft.com/canitpro/2015/07/07/step-by-step-changing-the-upn-suffix-for-an-entire-domain-via-powershell/

    This posting is provided AS IS with no warranties or guarantees , and confers no rights.

    Ahmed MALEK

    My Website Link

    My Linkedin Profile

    My MVP Profile

    Wednesday, April 26, 2017 7:31 AM
  • Hi Kalimanne,

    Is your problem resolved?

    / Karim

    Wednesday, April 26, 2017 12:08 PM
  • Use this code. I wrote it for you.

    Import-Module activedirectory
    Clear-Host
    $Users = Get-ADUser -Filter {(Mail -like "*@oldcompany.com") -or (physicalDeliveryOfficeName -like "*@oldcompany.com")} -Properties physicalDeliveryOfficeName,mail
    foreach($user in $Users)
    {
        try
        {  
            if ($user.mail)
            { 
                $OldMail   = $user.mail
                $NewMail = $OldMail.Replace("@oldcompany.com","@NewCompany.com")
                Set-ADUser $user -Replace @{"mail"=$NewMail} 
            }
    
            else
            {
                $OldOffice = $user.physicalDeliveryOfficeName
                $NewOffice = $OldOffice.Replace("@oldcompany.com","@NewCompany.com")
                Set-ADUser $user -Replace @{"physicalDeliveryOfficeName"=$NewOffice} 
            }
        }
        catch
        {
            Write-Warning "$_"
        }
    }


    Mahdi Tehrani | | www.mahditehrani.ir
    Make sure to download my free PowerShell scripts:

    • Proposed as answer by AlvwanModerator Tuesday, May 2, 2017 1:16 AM
    • Marked as answer by Kalimanne Tuesday, May 2, 2017 3:03 AM
    Wednesday, April 26, 2017 2:18 PM
    Moderator
  • > Their login ID is not changing.  They will still use our current domain name for their account login.

    Powershell comes to mind - get-adobject... Lot of hits about that on google/bing :)

    Wednesday, April 26, 2017 4:35 PM
  • Hi,

    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,

    Alvin Wang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, May 2, 2017 1:17 AM
    Moderator
  • Ok, I will try this later. It looks like it might work. So, I'll mark it as an answer.
    Tuesday, May 2, 2017 3:02 AM
  • There is now an error with my account.

    I can't create any new posts.  I can only reply to existing posts.

    I already tried going to the Forum Support forum, but I can't post there either.

    How can this be fixed?

    I had zero new posts today, but it says I exceeded the limit.

    • Your number of posts today has exceeded the limit.

    Tuesday, May 2, 2017 6:24 PM