How to Change a User’s Primary Email Address in Офіс 365 using PowerShell.
1. Run the PowerShell as administrator;
2. To run the scripts needed to change a user’s primary email address we must first set the script execution policy.
Get-ExecutionPolicy
3. The execution policy needs to be RemoteSigned. RemoteSigned allows only downloaded scripts signed by a trusted publisher to be run. If Get-ExecutionPolicy doesn’t return RemoteSigned, it must be changed by typing the following cmdlet and entering a “y” when prompted;
Set-ExecutionPolicy RemoteSigned
4. Now Get-ExecutionPolicy returns RemoteSigned;
5. Next we need to provide Офіс 365 with administrative credentials to be able to make changes. We’re going to assign our credentials to the variable $cred;
$cred = Get-Credential
6. And PowerShell prompts for an Офіс 365 account with administrative permissions;
7. After entering the credentials of an Офіс 365 administrator account and clicking добре, we need to open a connection to the Офіс 365 servers;
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection
8. Once the connection is open we need to import the commands for the Exchange Server shell;
Import-PSSession $Session
9. The following command sets a user’s primary email address from the current address to the desired address without changing the username;
Set-Mailbox -identity <user@current.com> -WindowsEmailAddress <user@new.com>
10. The Офіс 365 online administrative interface should show the new email address as the primary;
11. Don’t forget close session;
Remove-PSSession $Session