Office 365 – Update Primary Email Address in Bulk using PowerShell

In this post, I’ll go over steps on how to update users’ primary email address in bulk.

I had to update the custom domain name address for one of the tenants I manage. When the O365 tenant was setup, I didn’t have my domain name ready for various reasons and the users were setup with @{tenantname}.onmicrosoft.com addresses. Once I added the necessary DNS records for O365 and made sure my new domain name is listed as default in the domain tab(Microsoft 365 admin center -> Settings -> Domains) in the list of , I was ready to update the user accounts with the new domain name.

Before proceeding further make sure you are connected to Exchange Online,

$o365cred = Get-Credential
Connect-ExchangeOnline -credential $o365cred

To bulk update accounts from a csv file:

Note: csv file has User,Emailaddress as column headers. Enter the users’ email address with new domain in the csv file. It is strongly recommended to leave the onmicrosoft.com address in the users’ proxy addresses and specifying the new address as PrimarySmtpAddress with SMTP.

$users = Import-Csv C:\tmp\Update-Email\emails.csv
foreach ($user in $users){

$Mailbox= Get-Mailbox -Identity $user.User
$PrimaryEmail=$Mailbox.PrimarySmtpAddress
$SMTP ="SMTP:"+$user.Emailaddress
Set-Mailbox -Identity $user.User -EmailAddresses $SMTP,$PrimaryEmail -WindowsEmailAddress $user.Emailaddress -MicrosoftOnlineServicesID $user.Emailaddress 

}

To bulk update all accounts in the O365 tenant:

Note: This below script will change all accounts in the tenant from whatever your enter for the $oldDomain to the $newDomain. So, proceed with caution and comply with your change management process and steps.

$oldDomain = Read-Host "Enter existing domain name in '@domainname.com' format"
$newDomain = Read-Host "Enter new domain name in '@domainname.com' format"

$mailboxes = (Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox).where{$_.PrimarySmtpAddress -like "*$oldDomain"}
foreach ($mbx in $mailboxes){

$PrimaryEmail = $mbx.PrimarySmtpAddress
$newSMTPAddress = $mbx.PrimarySmtpAddress -split '@'
$newSMTPAddress = $newSMTPAddress[0] + $newDomain
$SMTP ="SMTP:"+$newSMTPAddress
Write-Host "Processing: $mbx.Name --> $newSMTPAddress"
Set-Mailbox -Identity $mbx.Identity -EmailAddresses $SMTP,$PrimaryEmail -WindowsEmailAddress $newSMTPAddress -MicrosoftOnlineServicesID $newSMTPAddress
}

You can also make this change in bulk in the portal as well,

  1. Select all the users whom you wish you update
  2. Click on Change domains
  3. Select the desired domain name from the drop down at the pop-up window
  4. Click Save changes

Thank you for stopping by.✌

AzureAD/O365 – Update User’s Manager using PowerShell

Organizational hierarchy is vital to run everyday activities smoothly in any enterprise. With Azure AD, user’s manager information can be updated easily and this information can be used in other features like Teams and apps that require workflow.

In this post, I’ll go over steps on how to update manager information for a specific user, for a list of users based on information from a csv file and to determine users’ manager information

Make sure you are connected to Azure AD,

$credential = Get-Credential
Connect-AzureAD -credential $credential

To update manager information for a specific user:

$user = Read-Host "Enter user's email address to update Manager information"
$Manager = Read-Host "Enter Manager's email address"
Set-AzureADUserManager -ObjectId (Get-AzureADUser -SearchString $User).Objectid -RefObjectId (Get-AzureADUser -SearchString $Manager).Objectid

To bulk update users’ manager information from a CSV file:

Note: csv file has UserEmail,ManagerEmail as column headers

$list = Import-csv "C:\tmp\SetManager\UsersManagers.csv"
Foreach($entry in $list) {
        $User = $entry.UserEmail
        $Manager = $entry.ManagerEmail
        $UserObj = Get-AzureADUser -SearchString $User
        $ManagerObj = Get-AzureADUser -SearchString $Manager

        Write-Host Updating $UserObj.DisplayName with $ManagerObj.DisplayName as manager -ForegroundColor Yellow

        Set-AzureADUserManager -ObjectId $UserObj.Objectid -RefObjectId $ManagerObj.Objectid

        Write-Host "Update Complete." -ForegroundColor Green

}

To determine a user’s manager:

$user = Read-Host "Enter user's email address to determine Manager information"
(Get-AzureADUserManager -ObjectId (Get-AzureADUser -SearchString $User).Objectid).DisplayName

To determine all users’ manager information in the Azure AD tenant:

$Users = Get-AzureADUser -All $true

ForEach($User in $Users) {

    $Manager = Get-AzureADUserManager -ObjectId $User.Objectid
    If ($Manager) {
    Write-Host $User.DisplayName manager is $Manager.DisplayName -ForegroundColor Green}
    Else {
    Write-Host "No Manager information found" for $User.DisplayName -ForegroundColor Red}
}

Thank you for stopping by. ✌