Need PowerShell Script - Swap two domains

Welcome Forums General PowerShell Q&A Need PowerShell Script - Swap two domains

This topic contains 6 replies, has 2 voices, and was last updated by

 
Participant
3 weeks, 5 days ago.

  • Author
    Posts
  • #112699

    Participant
    Points: 0
    Rank: Member

    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
    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, 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 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
     

  • #112712
    Jon

    Participant
    Points: 2
    Rank: Member

    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
    
     
    
    }
    
     
    
    
  • #112715

    Participant
    Points: 0
    Rank: Member

    So for me lets say it would look something like this

     

    $oldsuffix = "olddomain.com"

    $newsuffix = "newdomain.com"

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

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

    $_ | Set-ADUser -UserPrincipalName $newUpn

    }

     

    and the content of the text file would be

    user1

    user2

    user3

    user4

    etc...

    Thanks

  • #112717
    Jon

    Participant
    Points: 2
    Rank: Member

    Yep, that should work 😉

    Try against a few test accounts first though!

  • #112723

    Participant
    Points: 0
    Rank: Member

    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

    Adding Email addresses using PowerShell – Import from CSV file | Bulk mode | Office 365 | Part 5#13

    Thanks

     

  • #112732
    Jon

    Participant
    Points: 2
    Rank: Member

    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.

  • #112748

    Participant
    Points: 0
    Rank: Member

    ok here is what i will do next

    i will add a rename to a few users example

    john.doe@dax.com = > jdoe@dax.com

    and then i will also make 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

     

You must be logged in to reply to this topic.