none
powershell script to fix duplicate email addresses

    Question

  • I'm hoping this is possible to do through powershell.  I'm learning powershell but this is a bit too complex for me at this point.  Here is what I am trying to accomplish.  I need a script that will take a users primary email address, look at the prefix before the @domain.com and create a new email address with that prefix@newdomain.com.

    We applied a new email address policy to several thousands of users that created a new email address for them based on a new domain name that we are going to.  The issue is we have found a bunch of users with the same alias.  Not a big deal typically as Exchange will create the email address based off the alias and if two users have the same alias then exchange will append a number to the email address so there wont be two email addresses that are the same.  When we applied the new email address policy, Exchange did not do this for users with the same alias.  It just created the email address based on the alias and did not append a number to the email address if the same email address was already in use.  So now we have several users with duplicate email addresses for our new domain.  Any help is greatly appreciated.


    Rich

    Thursday, July 18, 2013 3:23 PM

Answers

  • Hi Rich

    Here's what I got:

    $users = Get-Mailbox TestUserName
    foreach ($u in $users) {
    $s = ((Get-Mailbox $u.alias).primarysmtpaddress).tostring().split("@")[0]
    $sj = $s+"@newdomain.com"
    Set-Mailbox $u.alias -EmailAddressPolicyEnabled $false -EmailAddresses (((Get-Mailbox $u.alias).EmailAddresses)+=$sj) -WhatIf
    }

    Save that as a .PS1 file and run it in the EMS - you might need to set the execution policy to remotesigned first:

    Set-ExecutionPolicy RemoteSigned

    Start with a test user (change the 'TestUserName') and the remove the -whatif. If you have nothing next to the Get-Mailbox command it will make this change against all your mailboxes so you might want to filter it down to certain OUs or by the initial letter of the identity (a* will do all the A's) - leave the -whatif on the end of the set-mailbox command if you test this bit.

    Let me know if you have any questions.

    Steve

     
    • Marked as answer by rich8722 Saturday, July 20, 2013 2:45 AM
    Friday, July 19, 2013 8:48 AM

All replies

  • Hi Rich

    Do you want the new address to replace the primary address or added as an additional SMTP address?

    Steve

    Thursday, July 18, 2013 3:51 PM
  • additional smtp address please

    Rich

    Thursday, July 18, 2013 3:53 PM
  • OK, I'll be near my test lab tomorrow morning and I'll knock something together for you unless someone else comes up with the goods before then.

    Steve

    Thursday, July 18, 2013 3:59 PM
  • Great, thanks a million.

    Rich

    Thursday, July 18, 2013 5:13 PM
  • Hi

    Some related script that might help

    finding users having duplicate MAIL addresses within same OU

    http://social.technet.microsoft.com/Forums/windowsserver/en-US/085064a1-ab2a-4de0-98b2-7fa180ae74d8/finding-users-having-duplicate-mail-addresses-within-same-ou

    “How to Use PowerShell to Fix Duplicate Email Addresses for On-Premises Mail-Enabled Objects”

    http://social.technet.microsoft.com/wiki/contents/articles/16686.how-to-use-powershell-to-fix-duplicate-email-addresses-for-on-premises-mail-enabled-objects.aspx

    Also the Script forum is a good place for asking powershell script

    http://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG

    Cheers

    If you have any feedback on our support, please click here


    Zi Feng
    TechNet Community Support

    Friday, July 19, 2013 8:21 AM
    Moderator
  • Hi Rich

    Here's what I got:

    $users = Get-Mailbox TestUserName
    foreach ($u in $users) {
    $s = ((Get-Mailbox $u.alias).primarysmtpaddress).tostring().split("@")[0]
    $sj = $s+"@newdomain.com"
    Set-Mailbox $u.alias -EmailAddressPolicyEnabled $false -EmailAddresses (((Get-Mailbox $u.alias).EmailAddresses)+=$sj) -WhatIf
    }

    Save that as a .PS1 file and run it in the EMS - you might need to set the execution policy to remotesigned first:

    Set-ExecutionPolicy RemoteSigned

    Start with a test user (change the 'TestUserName') and the remove the -whatif. If you have nothing next to the Get-Mailbox command it will make this change against all your mailboxes so you might want to filter it down to certain OUs or by the initial letter of the identity (a* will do all the A's) - leave the -whatif on the end of the set-mailbox command if you test this bit.

    Let me know if you have any questions.

    Steve

     
    • Marked as answer by rich8722 Saturday, July 20, 2013 2:45 AM
    Friday, July 19, 2013 8:48 AM
  • I will give this a shot and let you know, cant thank you enough.

    Rich

    Friday, July 19, 2013 12:26 PM
  • This works fine all on users except the ones with the duplicate aliases, on those it does not set the new email address.  This is the error it throws for each account that shares an alias with another account.:


    Rich


    • Edited by rich8722 Friday, July 19, 2013 1:16 PM picture
    Friday, July 19, 2013 1:12 PM
  • That error suggests that there is no primary SMTP address.  What happens if you just do:

    (Get-Mailbox username).primarysmtpaddress
    Where username is someone with a duplicate address.
    Friday, July 19, 2013 1:25 PM
  • it returns nothing if I run that against an alias that is in use by more than one user

    Rich

    Friday, July 19, 2013 1:36 PM
  • If you look at the Email Addresses tab for the user in EMC, does any address have SMTP next to it (rather than smtp)?
    Friday, July 19, 2013 1:51 PM
  • two users with duplicate aliases, this is what I see


    Rich

    Friday, July 19, 2013 2:00 PM
  • OK.  I'm not sure if it will make a difference but if you have users with the same alias we can use the UPN in the script instead:

    $users = Get-Mailbox TestUser
    foreach ($u in $users) {
    $s = ((Get-Mailbox $u.UserPrincipalName).primarysmtpaddress).tostring().split("@")[0]
    $sj = $s+"@otherdomain.com"
    Set-Mailbox $u.UserPrincipalName -EmailAddressPolicyEnabled $false -EmailAddresses (((Get-Mailbox $u.UserPrincipalName).EmailAddresses)+=$sj) -WhatIf
    }

    Friday, July 19, 2013 2:28 PM
  • If I replace .alias with .UserPrincipleName I cannot get the code to work.  Even if I do it on a normal user that does not have a duplicate alias it throws an error, which does work if I user .alias.


    Rich

    Friday, July 19, 2013 2:58 PM
  • Its the same error fyi too

    Rich

    Friday, July 19, 2013 3:00 PM
  • $users = Get-Mailbox asolis2@testdomain.org
    $s = ((Get-Mailbox asolis2@testdomain.org).primarysmtpaddress).tostring().split("@")[0]
    $sj = $s+"@newdomain.org"
    Set-Mailbox asolis2@testdomain.org -EmailAddressPolicyEnabled $false -EmailAddresses (((Get-Mailbox asolis2@testdomain.org).EmailAddresses)+=$sj)
    Ok, so I modified your code a little to get this to work on one of the accounts that has the same alias as another account by specifying the email address for that account in order to grab that mailbox.  Is there a way to create a variable for that email address, then pull email addresses for that variable from a .CSV file then pipe it into this script?

    Rich

    Friday, July 19, 2013 5:01 PM
  • Ok, below is what Ive come up with, it works great reading from the csv file for one user, but when it goes to the next line the variable $email then contains both email addresses and it of course fails.  I need to be able to clear out the variable and have it loop back through the next line of the CSV file.

    Import-Csv c:\test.csv |`
        ForEach-Object {
            $email += $_.Email
           
        }

    $users = Get-Mailbox $email
    $s = ((Get-Mailbox $email).primarysmtpaddress).tostring().split("@")[0]
    $sj = $s+"@newdomain.org"
    Set-Mailbox $email -EmailAddressPolicyEnabled $false -EmailAddresses (((Get-Mailbox $email).EmailAddresses)+=$sj)


    Rich

    Friday, July 19, 2013 6:23 PM
  • I think I finally got it.  This works as I need it to, I'll just need to export the email addresses to a CSV and use the below code:

    $CSV = Import-Csv c:\test.csv
    ForEach ($line in $CSV)
    {
           
            $users = Get-Mailbox $line.Email
    $s = ((Get-Mailbox $line.Email).primarysmtpaddress).tostring().split("@")[0]
    $sj = $s+"@newdomain.org"
    Set-Mailbox $line.Email -EmailAddressPolicyEnabled $false -EmailAddresses (((Get-Mailbox $line.Email).EmailAddresses)+=$sj)

     


    Rich


    • Edited by rich8722 Monday, July 22, 2013 7:32 PM fix name
    Friday, July 19, 2013 8:22 PM
  • Great, glad you got it working!

    Steve

    Monday, July 22, 2013 8:58 AM