# Need PowerShell Script - Swap two domains

**URL:** <https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379>\
**Category:** PowerShell Help\
**Created:** [September 26, 2018, 4:50am UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379 "2018-09-26T04:50:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxmax2](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@maxmax2](https://forums.powershell.org/u/maxmax2)\
**Post date:** [September 26, 2018, 4:50am UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379/1 "2018-09-26T04:50:39Z")

</div>

I have been now searching for a script that would switch primary email between two domains, using Office 365 Exchange online  
example  
each user has a primary and alias email attached to his account,  
user@domainA.com - is primary  
user@domainB.com - is alias  
i wish to switch and make @domainB.com the primary and the A as alias  
been trying to find and it seems simple, but yeah i would need help 🙂

i will add a few more points that were done before.  
The tenant has 3 domains: A, B, C, i only focused on users with A mad B  
I followed this guide - [https://gallery.technet.microsoft.com/office/Bulk-Add-SMTP-Addresses-to-e3d28842](https://gallery.technet.microsoft.com/office/Bulk-Add-SMTP-Addresses-to-e3d28842)  
and added the B domain to A domain users, (this did not create a SIP btw, is there another script for this as well?)  
Before and after the guide

example: i had 2 domains with their own users, no aliases shared  
users@companyA.com  
users@companyC.com

After the guide and Added a new domain with user names to [companyA.com](https://companya.com/), so it looks like this  
users@companyA.com + users@companyB.com  
users@companyC.com

in the guide above i managed to create a filter, like this  
#Get the list of mailboxes in the Office 365 tenant  
$Mailboxes = @(Get-Mailbox -Filter { WindowsEmailAddress -like “\*@companyA.com” })

so when the script executed it did it all just for these users, [companyC.com](https://companyc.com/) was not affected.  
now what needs to be done is switching the main / primary domain for the users as explained in the first part.  
Let me know if i can add more details, i found a lot of scripts but the mostly add new things or do some other changes, all i need to make the switch between alias and primary,

Thanks  
&nbsp;

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [September 26, 2018, 5:45am UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379/2 "2018-09-26T05:45:20Z")

</div>

I believe you are just trying to change the UPN right?

If so something like this should work, where upns.txt is a text file with the usernames of accounts to be changed

```
$oldsuffix = "oldupn.com"

$newsuffix = "newupn.com"

gc C:\scripts\upns.txt | Get-ADUser | ForEach-Object {

 

$newUpn = $_.UserPrincipalName.Replace($oldSuffix,$newSuffix)

 

$_ | Set-ADUser -UserPrincipalName $newUpn

 

}

 
```

---

<div class="post-metadata">

**Author:** ![maxmax2](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@maxmax2](https://forums.powershell.org/u/maxmax2)\
**Post date:** [September 26, 2018, 6:34am UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379/3 "2018-09-26T06:34:25Z")

</div>

So for me lets say it would look something like this

&nbsp;

$oldsuffix = “[olddomain.com](http://olddomain.com)”

$newsuffix = “[newdomain.com](http://newdomain.com)”

gc C:\scripts\upns.txt | Get-ADUser | ForEach-Object {

$newUpn = $\_.UserPrincipalName.Replace($oldSuffix,$newSuffix)

$\_ | Set-ADUser -UserPrincipalName $newUpn

}

&nbsp;

and the content of the text file would be

user1

user2

user3

user4

etc…

Thanks

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [September 26, 2018, 7:16am UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379/4 "2018-09-26T07:16:30Z")

</div>

Yep, that should work 😉

Try against a few test accounts first though!

---

<div class="post-metadata">

**Author:** ![maxmax2](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@maxmax2](https://forums.powershell.org/u/maxmax2)\
**Post date:** [September 26, 2018, 7:28am UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379/5 "2018-09-26T07:28:03Z")

</div>

Since i am new to powershell, this would be a script file that is calling the text file right? also i just came across this link and i think it might fit the requirement

[https://o365info.com/adding-email-addresses-using-powershell-import-from-csv-file-bulk-mode-office-365-part-5-13/](https://o365info.com/adding-email-addresses-using-powershell-import-from-csv-file-bulk-mode-office-365-part-5-13/)

Thanks

&nbsp;

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [September 26, 2018, 7:58am UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379/6 "2018-09-26T07:58:05Z")

</div>

There is a lot of code on that page, you need be more specific.

Which specific attribute(s) in AD are you trying to update.

---

<div class="post-metadata">

**Author:** ![maxmax2](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@maxmax2](https://forums.powershell.org/u/maxmax2)\
**Post date:** [September 26, 2018, 8:30am UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379/7 "2018-09-26T08:30:52Z")

</div>

ok here is what i will do next

i will add a rename to a few users example

[john.doe@dax.com](mailto:john.doe@dax.com) = \> [jdoe@dax.com](mailto:jdoe@dax.com)

and then i will also make [jdoe@dax.com](mailto:jdoe@dax.com) as the primary email

and i will also have to switch all the other users @dax.com to be primary email

so for some i will do a rename and for some i will switch from @bax.com =\> @dax.com

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/need-powershell-script-swap-two-domains/11379/8 "2024-05-16T20:34:20Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


