Link to home
Start Free TrialLog in
Avatar of Alexandre Michel
Alexandre MichelFlag for Australia

asked on

Office 365 script to change username & main email address of certain mailboxes

Hi Expert

I have an Office 365 that uses several domains (@domain1.com, @domain2.com, @domain3.com, @domain4.com, etc...)
Mailboxes can can have one or more aliases using these domains.

I am wanting to change the user principal email address of all users whose current user principal email address is @domain2.com to become @domain3.com. I do not want to change any mailboxes that have @domain1.com or @domain3.com as their UserPrincipalName

If possible, I would like to also search for and add the @domain2.com domain with there are aliases using @domain2.com

... And finally, I would like to have a test mode so I can do a test run before I go and blast every mailboxes in this tenancy with the wrong details!

Here are are a couple of examples

john@domain1.com
  • Stays as is

jane@domain2.com
  • Add jane@domain3.com,
  • Make jane@domain3.com the UserPrincipalName,
  • and leave jane@domain2.com as an alias

peter@domain2.com which has also an alias of paul@domain2.com
  • Add peter@domain3.com and paul@domain3.com to the mailbox
  • Leave peter@domain2.com & paul@domain2.com as aliases
  • Make peter@domain3.com his UserPrincipalName


Note: I did some reasearch before posting this question. I found something similar in https://o365info.com/adding-email-addresses-using-powershell-bulk-mode-office-365-part-4-13/ ... but it goes through ALL the mailboxes rather than the mailboxes with one given domain.

Can you assist?

Thanks

Alexandre
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

You can simply filter based on the UPN:

Get-MsolUser -All | ? {$_.UserPrincipalName -like "*@domain2.com"}

Open in new window

Avatar of Alexandre Michel

ASKER

Hi Vasil,

Thanks for that . So the original script I found in here is:

$AllMailboxes = Get-Mailbox -ResultSize Unlimited
Foreach ($Mailbox in $AllMailboxes)
{
     $NewAddress = $Mailbox.Alias + "@o365pilot.com"
     Set-Mailbox -Identity $Mailbox.Alias -WindowsEmailAddress $NewAddress #-whatif
}

Open in new window

And I should change the 1st line to this

$AllMailboxes = Get-MsolUser -All | ? {$_.UserPrincipalName -like "*@domain2.com"}
Foreach ($Mailbox in $AllMailboxes)
{
     $NewAddress = $Mailbox.Alias + "@o365pilot.com"
     Set-Mailbox -Identity $Mailbox.Alias -WindowsEmailAddress $NewAddress #-whatif
}

Open in new window

Is that correct?
ASKER CERTIFIED SOLUTION
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the feedback Vasil. At the end I had to Google around to get the full answer.