Microsoft Account Errors

Change Microsoft 365 User Sign-In Domains by Department Using CSV and PowerShell

John WilsonJohn Wilson Nov 27, 2026 868 views

Question details

An administrator needs to change the Microsoft 365 sign-in domain for users in a specific department using a CSV file and PowerShell.

Change Microsoft 365 User Sign-In Domains by Department Using CSV and PowerShell
Product
Microsoft 365 and Microsoft Graph PowerShell
Device & OS
not provided
Scenario
An organization has multiple domains and wants to update the UserPrincipalName domain for a selected department in bulk.
Observed behavior
The target users should be identified by department and assigned sign-in names using the new verified domain.
Before you start

Confirm that the target domain is verified in Microsoft 365, that the department values are accurate, and that the account has permission to update the selected users. Determine whether the accounts are synchronized from on-premises Active Directory; synchronized users generally need their UPN changed in the source directory.

Solution 1Recommended

Update cloud-only user sign-in domains with Microsoft Graph PowerShell

Use this method to export department members, review their proposed sign-in names, and apply the changes from a CSV file.

Microsoft Graph PowerShell replaces the retired MSOnline PowerShell module. The example targets cloud-only member accounts and preserves the portion of each sign-in name before the @ symbol.

A UPN change affects the sign-in name; it does not automatically change the user's primary email address or proxy addresses. Review those addresses and license requirements separately.

1
Connect to Microsoft Graph

In PowerShell, install the module if needed with Install-Module Microsoft.Graph -Scope CurrentUser, then run Connect-MgGraph -Scopes 'User.ReadWrite.All'. Sign in with an appropriately privileged administrator account and grant the requested consent.

2
Export the department list

Replace Finance and example.com with the department and verified target domain, then run: $department = 'Finance' $domain = 'example.com' Get-MgUser -All -Property Id,UserPrincipalName,Department,UserType,OnPremisesSyncEnabled | Where-Object { $_.Department -eq $department -and $_.UserType -eq 'Member' -and $_.OnPremisesSyncEnabled -ne $true } | Select-Object Id,UserPrincipalName,Department,@{Name='NewUserPrincipalName';Expression={($_.UserPrincipalName -split '@')[0] + '@' + $domain}} | Export-Csv -Path './department-users.csv' -NoTypeInformation -Encoding UTF8

3
Review the proposed changes

Open department-users.csv in a spreadsheet editor or run Import-Csv './department-users.csv' in PowerShell. Confirm each user and NewUserPrincipalName, check for duplicate or existing sign-in names, and verify that related proxy addresses and licenses meet your organization's requirements.

4
Apply the reviewed CSV

After saving the CSV with its column headers intact, run: Import-Csv './department-users.csv' | ForEach-Object { try { Update-MgUser -UserId $_.Id -UserPrincipalName $_.NewUserPrincipalName -ErrorAction Stop Write-Host "Updated $($_.UserPrincipalName) to $($_.NewUserPrincipalName)" } catch { Write-Warning "Failed for $($_.UserPrincipalName): $_" } }

5
Verify the results

In the Microsoft 365 admin center, open Users > Active users and check the updated usernames. Review any PowerShell warnings and let affected users know their new sign-in names.

Update cloud-only user sign-in domains with Microsoft Graph PowerShell
Synchronized accounts: Change synchronized users' UPNs in the on-premises source directory and allow directory synchronization to propagate the updates. Do not rely on the cloud-only script for those accounts.
Free Microsoft Office alternative

Review Your Microsoft 365 User CSV in WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar interface. Open and review your user list in WPS Spreadsheets while keeping compatibility with Microsoft Office formats for an easy move between applications. Microsoft Graph PowerShell is still required to update the accounts.

Review department records and proposed sign-in names in a CSV fileCompatible with Microsoft Excel formats, including XLSXUse a familiar spreadsheet interface for filtering and editingKeep the Microsoft 365 account update separate from CSV preparation
microsoft office alternative - wps office

Frequently Asked Questions

Does changing the UPN also change the user's email address?

Not automatically. The UPN is the sign-in name; review the primary email address and proxy addresses separately in the appropriate Microsoft 365 or Exchange administration tools.

Can I use this script for synchronized users?

The example excludes users marked as synchronized. For those accounts, change the UPN in the on-premises source directory and allow synchronization to update Microsoft 365.

What if the new sign-in name is already in use?

The update can fail if the proposed UPN is not unique. Review the NewUserPrincipalName column and check existing accounts before running Update-MgUser.

Why does the example use Microsoft Graph instead of Get-MsolUser?

The MSOnline PowerShell module is retired. Microsoft Graph PowerShell provides commands such as Get-MgUser and Update-MgUser for this workflow.

Do users need to know about the sign-in name change?

Yes. Tell affected users their new UPN and verify the results under Users > Active users in the Microsoft 365 admin center so they know which name to use when signing in.