SOLVED

Powershell for replacing primary email address and adding current primary to alias email addresses

%3CLINGO-SUB%20id%3D%22lingo-sub-197844%22%20slang%3D%22en-US%22%3EPowershell%20for%20replacing%20primary%20email%20address%20and%20adding%20current%20primary%20to%20alias%20email%20addresses%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-197844%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20use%20a%20hybrid%20O365%20environment.%26nbsp%3B%20On-premise%20AD%2C%20using%20Azure%20AD%26nbsp%3Bsync%2C%20with%20all%20O365%20mailboxes.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20have%20about%20120%20mailboxes%20where%20we%20need%20to%20change%20their%20primary%20email%26nbsp%3Bdomain%26nbsp%3Bto%20a%20new%20domain%20and%20ADD%20the%20existing%20primary%20email%20address%20as%20an%20alias%2C%20without%20deleting%20the%20existing%20aliases.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20would%20like%20to%20use%26nbsp%3Bpowershell%2C%20importing%20from%26nbsp%3Ba%20.csv%20file%2C%20but%20cannot%20determine%20the%20powershell%20syntax%20needed%20to%20handle%20both%20the%20primary%20replacement%2C%20and%26nbsp%3Bthe%20addition%20of%20the%20current%20primary%20to%20the%20proxyaddresses%20field%2C%20without%20wiping%20out%20the%20existing%20proxyaddresses%20list.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20were%20also%20considering%20running%20two%20separate%20powershells%20to%20handle%20this.%26nbsp%3B%20The%20first%20one%20would%26nbsp%3Breplace%20the%20existing%20primary%20with%20the%20new%20email%20address%2C%20but%20we%20read%20that%20when%20you%20do%20that%2C%20it%20automatically%20adds%20the%20existing%20primary%20as%20an%20alias%2C%20which%20is%20what%20we%20want%2C%20but%20deletes%20the%20existing%20aliases%2C%20which%20we%20definitely%20do%20not%20want.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20with%20a%20single%20script%20that%20would%20replace%20the%20primary%2C%20and%20ADD%20the%20existing%20primary%20as%20an%20alias%20would%20be%20appreciated.%26nbsp%3B%20If%20a%20single%20script%20can't%20do%20this%2C%20is%20there%20a%20way%20to%20replace%20the%20existing%20primary%20with%20a%20new%20email%20address%20that%20does%20not%20wipe%20out%20the%20existing%20proxyaddresses%20list%20during%20the%20process%3F%26nbsp%3B%20We%20would%20then%20run%20a%20second%20powershell%20to%20add%20an%20alias%20to%20the%20list.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20-%20April%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-197844%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-198474%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20for%20replacing%20primary%20email%20address%20and%20adding%20current%20primary%20to%20alias%20email%20address%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-198474%22%20slang%3D%22en-US%22%3E%3CP%3EWhat%20a%20great%2C%20thorough%20response.%26nbsp%3B%20Thank%20you%20for%20your%20assistance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EApril%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-198315%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20for%20replacing%20primary%20email%20address%20and%20adding%20current%20primary%20to%20alias%20email%20address%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-198315%22%20slang%3D%22en-US%22%3E%3CP%3EOn-premises%2C%20you%20should%20be%20able%20to%20handle%20this%20via%20Email%20Address%20Policies%3A%20%3CA%20href%3D%22https%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fbb232171(v%3Dexchg.150).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fbb232171(v%3Dexchg.150).aspx%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt's%20also%20possible%20via%20PowerShell%2C%20the%20easiest%20example%20would%20be%20by%20using%20the%20-WindowsEmailAddress%20parameter%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22lia-spoiler-container%22%3E%3CA%20class%3D%22lia-spoiler-link%22%20href%3D%22%23%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3ESpoiler%3C%2FA%3E%3CNOSCRIPT%3E(Highlight%20to%20read)%3C%2FNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-border%22%3E%3CDIV%20class%3D%22lia-spoiler-content%22%3E%0A%3CDIV%20class%3D%22parameterName%20stack%22%20style%3D%22padding%3A%200px%206px%202px%3B%20color%3A%20rgb(0%2C%200%2C%200)%3B%20text-transform%3A%20none%3B%20text-indent%3A%200px%3B%20letter-spacing%3A%20normal%3B%20font-family%3A%20Consolas%2C%20Menlo%2C%20Monaco%2C%20%26quot%3BLucida%20Console%26quot%3B%2C%20%26quot%3BLiberation%20Mono%26quot%3B%2C%20%26quot%3BDejaVu%20Sans%20Mono%26quot%3B%2C%20%26quot%3BBitstream%20Vera%20Sans%20Mono%26quot%3B%2C%20%26quot%3BCourier%20New%26quot%3B%2C%20monospace%2C%20sans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-weight%3A%20bold%3B%20margin-top%3A%2024px%3B%20word-spacing%3A%200px%3B%20display%3A%20inline-block%3B%20white-space%3A%20normal%3B%20orphans%3A%202%3B%20widows%3A%202%3B%20background-color%3A%20rgb(232%2C%20235%2C%20240)%3B%20font-variant-ligatures%3A%20normal%3B%20font-variant-caps%3A%20normal%3B%20-webkit-text-stroke-width%3A%200px%3B%20text-decoration-style%3A%20initial%3B%20text-decoration-color%3A%20initial%3B%22%3E-WindowsEmailAddress%3C%2FDIV%3E%0A%3CDIV%20class%3D%22parameterInfo%22%20style%3D%22color%3A%20rgb(0%2C%200%2C%200)%3B%20text-transform%3A%20none%3B%20text-indent%3A%200px%3B%20letter-spacing%3A%20normal%3B%20font-family%3A%20segoe-ui_normal%2C%20%26quot%3BSegoe%20UI%26quot%3B%2C%20Segoe%2C%20%26quot%3BSegoe%20WP%26quot%3B%2C%20%26quot%3BHelvetica%20Neue%26quot%3B%2C%20Helvetica%2C%20sans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-weight%3A%20400%3B%20margin-left%3A%2024px%3B%20word-spacing%3A%200px%3B%20white-space%3A%20normal%3B%20orphans%3A%202%3B%20widows%3A%202%3B%20background-color%3A%20rgb(255%2C%20255%2C%20255)%3B%20font-variant-ligatures%3A%20normal%3B%20font-variant-caps%3A%20normal%3B%20-webkit-text-stroke-width%3A%200px%3B%20text-decoration-style%3A%20initial%3B%20text-decoration-color%3A%20initial%3B%22%3E%0A%3CP%20style%3D%22margin-top%3A%201rem%3B%20margin-bottom%3A%200px%3B%22%3EThe%20WindowsEmailAddress%20parameter%20specifies%20the%20Windows%20email%20address%20for%20this%20recipient.%20This%20is%20a%20common%20Active%20Directory%20attribute%20that's%20present%20in%20all%20environments%2C%20including%20environments%20without%20Exchange.%20Using%20the%20WindowsEmailAddress%20parameter%20on%20a%20recipient%20has%20one%20of%20the%20following%20results%3A%3C%2FP%3E%0A%3CUL%20style%3D%22margin%3A%2016px%200px%2016px%2038px%3B%20padding%3A%200px%3B%22%3E%0A%3CLI%3E%3CP%20style%3D%22margin-top%3A%201rem%3B%20margin-bottom%3A%200px%3B%22%3EIn%20environments%20where%20the%20recipient%20is%20subject%20to%20email%20address%20policies%20(the%20EmailAddressPolicyEnabled%20property%20is%20set%20to%20the%20value%20True%20for%20the%20recipient)%2C%20the%20WindowsEmailAddress%20parameter%20has%20no%20effect%20on%20the%20WindowsEmailAddress%20property%20or%20the%20primary%20email%20address%20value.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%20style%3D%22margin-top%3A%201rem%3B%20margin-bottom%3A%200px%3B%22%3EIn%20environments%20where%20the%20recipient%20isn't%20subject%20to%20email%20address%20policies%20(the%20EmailAddressPolicyEnabled%20property%20is%20set%20to%20the%20value%20False%20for%20the%20recipient)%2C%20the%20WindowsEmailAddress%20parameter%20updates%20the%20WindowsEmailAddress%20property%20and%20the%20primary%20email%20address%20to%20the%20same%20value.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20style%3D%22margin-top%3A%201rem%3B%20margin-bottom%3A%200px%3B%22%3EThe%20WindowsEmailAddress%20property%20is%20visible%20for%20the%20recipient%20in%20Active%20Directory%20Users%20and%20Computers%20in%20the%20E-mail%20attribute.%20The%20attribute%20common%20name%20is%20E-mail-Addresses%2C%20and%20the%20Ldap-Display-Name%20is%20mail.%20If%20you%20modify%20this%20attribute%20in%20Active%20Directory%2C%20the%20recipient's%20primary%20email%20address%20is%20not%20updated%20to%20the%20same%20value.%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%3CNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-noscript-container%22%3E%3CDIV%20class%3D%22lia-spoiler-noscript-content%22%3E-WindowsEmailAddress%20The%20WindowsEmailAddress%20parameter%20specifies%20the%20Windows%20email%20address%20for%20this%20recipient.%20This%20is%20a%20common%20Active%20Directory%20attribute%20that's%20present%20in%20all%20environments%2C%20including%20environments%20without%20Exchange.%20Using%20the%20WindowsEmailAddress%20parameter%20on%20a%20recipient%20has%20one%20of%20the%20following%20results%3A%20In%20environments%20where%20the%20recipient%20is%20subject%20to%20email%20address%20policies%20(the%20EmailAddressPolicyEnabled%20property%20is%20set%20to%20the%20value%20True%20for%20the%20recipient)%2C%20the%20WindowsEmailAddress%20parameter%20has%20no%20effect%20on%20the%20WindowsEmailAddress%20property%20or%20the%20primary%20email%20address%20value.%20In%20environments%20where%20the%20recipient%20isn't%20subject%20to%20email%20address%20policies%20(the%20EmailAddressPolicyEnabled%20property%20is%20set%20to%20the%20value%20False%20for%20the%20recipient)%2C%20the%20WindowsEmailAddress%20parameter%20updates%20the%20WindowsEmailAddress%20property%20and%20the%20primary%20email%20address%20to%20the%20same%20value.%20The%20WindowsEmailAddress%20property%20is%20visible%20for%20the%20recipient%20in%20Active%20Directory%20Users%20and%20Computers%20in%20the%20E-mail%20attribute.%20The%20attribute%20common%20name%20is%20E-mail-Addresses%2C%20and%20the%20Ldap-Display-Name%20is%20mail.%20If%20you%20modify%20this%20attribute%20in%20Active%20Directory%2C%20the%20recipient's%20primary%20email%20address%20is%20not%20updated%20to%20the%20same%20value.%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FNOSCRIPT%3E%3C%2FDIV%3E%3C%2FDIV%3E%0A%3CP%3EDoing%20so%20will%20add%20the%20new%20primary%20SMTP%20address%2C%20while%20preserving%20the%20old%20one%20as%20secondary.%20Alternatively%20you%20can%20cook%20up%20a%20more%20complex%20function%20to%20check%20out%20each%20individual%20address%20and%20add%2Fremove%20new%20ones%20as%20needed.%20I%20have%20some%20examples%20on%20how%20to%20do%20this%20here%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.michev.info%2FBlog%2FPost%2F2011%2Fbulk-replacing-proxy-addresses-based-on-a-pattern%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.michev.info%2FBlog%2FPost%2F2011%2Fbulk-replacing-proxy-addresses-based-on-a-pattern%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

We use a hybrid O365 environment.  On-premise AD, using Azure AD sync, with all O365 mailboxes.

 

We have about 120 mailboxes where we need to change their primary email domain to a new domain and ADD the existing primary email address as an alias, without deleting the existing aliases.

 

We would like to use powershell, importing from a .csv file, but cannot determine the powershell syntax needed to handle both the primary replacement, and the addition of the current primary to the proxyaddresses field, without wiping out the existing proxyaddresses list.

 

We were also considering running two separate powershells to handle this.  The first one would replace the existing primary with the new email address, but we read that when you do that, it automatically adds the existing primary as an alias, which is what we want, but deletes the existing aliases, which we definitely do not want.

 

Any help with a single script that would replace the primary, and ADD the existing primary as an alias would be appreciated.  If a single script can't do this, is there a way to replace the existing primary with a new email address that does not wipe out the existing proxyaddresses list during the process?  We would then run a second powershell to add an alias to the list.

 

Thank you - April

2 Replies
Highlighted
Solution

On-premises, you should be able to handle this via Email Address Policies: https://technet.microsoft.com/en-us/library/bb232171(v=exchg.150).aspx

 

It's also possible via PowerShell, the easiest example would be by using the -WindowsEmailAddress parameter:

 

Spoiler
-WindowsEmailAddress

The WindowsEmailAddress parameter specifies the Windows email address for this recipient. This is a common Active Directory attribute that's present in all environments, including environments without Exchange. Using the WindowsEmailAddress parameter on a recipient has one of the following results:

  • In environments where the recipient is subject to email address policies (the EmailAddressPolicyEnabled property is set to the value True for the recipient), the WindowsEmailAddress parameter has no effect on the WindowsEmailAddress property or the primary email address value.

  • In environments where the recipient isn't subject to email address policies (the EmailAddressPolicyEnabled property is set to the value False for the recipient), the WindowsEmailAddress parameter updates the WindowsEmailAddress property and the primary email address to the same value.

The WindowsEmailAddress property is visible for the recipient in Active Directory Users and Computers in the E-mail attribute. The attribute common name is E-mail-Addresses, and the Ldap-Display-Name is mail. If you modify this attribute in Active Directory, the recipient's primary email address is not updated to the same value.

Doing so will add the new primary SMTP address, while preserving the old one as secondary. Alternatively you can cook up a more complex function to check out each individual address and add/remove new ones as needed. I have some examples on how to do this here:

 

https://www.michev.info/Blog/Post/2011/bulk-replacing-proxy-addresses-based-on-a-pattern

Highlighted

What a great, thorough response.  Thank you for your assistance.

 

April