I have a client that I have partner admin login for Office 365 online. They have added a new domain and have created alias email addresses with the new domain. Now they want to make the alias email address the primary and the primary the alias. I have pieced together a script but am concerned that it will actually add the new domain in a second time. Please look at the script and help me make this thing work right (correct) the first time. Thanks.
Supply User credentials with admin privileges
$cred = Get-Credential
Initial connection to Partner Exchange Online
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/PowerShell-LiveID?DelegatedOrg=(currentprimarydomain).onmicrosoft.com -Credential $cred -Authentication Basic –AllowRedirection
Final connection to Exchange Online
Import-PSSession $Session
Define the new primary domain
$NewEmailDomain = ‘@(currentaliasdomain)’
Get a list of mailboxes and store in a variable
$Mailboxes = Get-Mailbox -ResultSize Unlimited
Change the primary email address for each mailbox to the new domain
foreach ($Mailbox in $Mailboxes)
{
$Address = “smtp:$($Mailbox.Alias)$NewEmailDomain”
$Mailbox.EmailAddresses.Add($Address)
Set-Mailbox -Identity $Mailbox.Identity -EmailAddresses $Mailbox.EmailAddresses -WindowsEmailAddress $Address
}
Close Session when finished
Remove-PSSession $Session