Exchange Online PS Assistance

Hi all

Can anyone assist me with a few simple PS commands for EOL? It is for around 600 users that are getting an additional email alias.

Basically what I want to achieve is the following, seems using a properly formatted CSV is the easiest way

  1. Add an alias to a mailbox (this I have managed to figure out)
    1. Import-CSV "C:\Scripts\AddEmailAddress\AddEmailAddress.csv" | ForEach {Set-Mailbox $_.Mailbox -EmailAddresses @{add=$_.NewEmailAddress}}
  2. Set the new alias as primary
  3. Set the new primary alias as their login
I need help with 2 and 3 and I would like to use a CSV file as the source for everything as it is the current data format.

Thank you

I Never worked on exchange, but below links will help you to get it done.

Setting alias as primary - https://techcommunity.microsoft.com/t5/office-365/powershell-for-replacing-primary-email-address-and-adding/m-p/197844

I think the alias can be used for login by default (I use alias to login to my outlook account).

You can have all relevant data in csv and can consume the attributes in the same way you did for step 1 in the same Foreach-Object scriptblock for step 2.

[quote quote=244346]I Never worked on exchange, but below links will help you to get it done.

Setting alias as primary – https://techcommunity.microsoft.com/t5/office-365/powershell-for-replacing-primary-email-address-and-adding/m-p/197844

I think the alias can be used for login by default (I use alias to login to my outlook account).

You can have all relevant data in csv and can consume the attributes in the same way you did for step 1 in the same Foreach-Object scriptblock for step 2.

[/quote]

Thank you, I am a total noob at this and those links are quite confusing unfortunately.

I’ve messed up formatting on my previous replies and I guess they were scraped, so here is my reply with proper formatting:

Hello Mark,

Set the new alias as primary:
EmailAddresses property show all emails for the mailbox. Primary email is prefixed with SMTP (all caps) and all aliases are prefixed with smtp (all lowercase).
Example:

SMTP: myprimaryemail@mycompany.com
smtp: myalias1@mycompany.com

To set some email as Primary you have 3 options:

  1. Use the SMTP prefix on the address
Set-Mailbox $Mailbox -EmailAddresses
smtp: myalias1@mycompany.com, SMTP: myprimaryemail@mycompany.com
  1. The first email address when you don’t use any prefix values
Set-Mailbox $Mailbox -EmailAddresses myprimaryemail@mycompany.com, myalias1@mycompany.com, myalias2@mycompany.com

So in your case, because you are adding email address to already existing collection of email addresses I would get current primary email address, remove it from collection and added new one with SMTP prefix and old one with smtp prefix.
Something like this:

$OldPrimary=Get-Mailbox $Mailbox | Select-Object -ExpandProperty PrimarySmtpAddress
Set-Mailbox $Mailbox -EmailAddresses
@{add="SMTP:newprimary@company.com","smtp:$OldPrimary";remove="SMTP:$OldPrimary"}

Reference:
https://docs.microsoft.com/en-us/powershell/module/exchange/set-mailbox?view=exchange-ps

https://docs.microsoft.com/en-us/exchange/modifying-multivalued-properties-exchange-2013-help

Regarding #3 please provide more details.

Hope that helps.

 

Thank you, this does make sense but how would I script it for 600+ users when not all of them will be receiving the new primary?

I also considered simply using this command to set them all.

Set-Mailbox -identity currentprimary@xxx.com -WindowsEmailAddress newprimary@yyy.com (I am struggling trying to figure out how to use an input file with the old and new addresses like I did with adding the new address)

Point 3 was their AAD logon that needs to be updated to the same as their primary SMTP.

 

 

Mark,

On order to script it simply rum single command in the loop.

As for the #3:

Set-MsolUserPrincipalName -UserPrincipalName "davidc@contoso.com" -NewUserPrincipalName "davidchew@contoso.com"

Hope that helps.

Reference: https://docs.microsoft.com/en-us/powershell/module/exchange/set-mailbox?view=exchange-ps

 

[quote quote=244805]Mark,

On order to script it simply rum single command in the loop.

As for the #3:

Set-MsolUserPrincipalName -UserPrincipalName “davidc@contoso.com” -NewUserPrincipalName “davidchew@contoso.com” Hope that helps.

Reference: https://docs.microsoft.com/en-us/powershell/module/exchange/set-mailbox?view=exchange-ps

[/quote]

Thank you, I also found this as well which seems to work pretty well.

Set-AzureADUser -objectid old@upn.com -UserPrincipalName new@upn.com