Microsoft 365 Error Codes

Fix Invalid UserPrincipalName Errors When Importing Microsoft 365 Users

Huda QurayshiHuda Qurayshi Oct 13, 2026 869 views

Question details

The user needs to fix syntax and encoding errors related to an invalid UserPrincipalName when attempting to import multiple users into Microsoft 365 using a CSV file and PowerShell scripts.

Fix Invalid UserPrincipalName Errors When Importing Microsoft 365 Users
Product
Microsoft 365
Device & OS
not provided
Scenario
Importing new users into a Microsoft 365 tenant using a CSV file and the New-MsolUser PowerShell cmdlet.
Observed behavior
Some CSV records are rejected with an invalid USERPRINCIPALNAME syntax error during the import, while other records in the same file are processed successfully.
Before you start

Ensure you have administrator access to your Microsoft 365 tenant and verify that the domains used in your CSV file are fully registered and active in your directory.

Solution 1Recommended

Clean and Validate CSV Data Formatting

Resolve formatting and syntax issues that trigger the UserPrincipalName error by thoroughly sanitizing the CSV file.

The most common cause of this error is hidden formatting issues within the CSV file, such as trailing spaces, unsupported characters, or incorrect encoding formats that PowerShell cannot parse correctly.

1
Check for Invalid Characters and Spaces

Open your CSV file in a spreadsheet editor. Review the UserPrincipalName column and use the 'Find and Replace' tool to remove any accidental spaces before or after the email addresses.

2
Verify Domain Names

Ensure all usernames use a verified domain registered in your Microsoft 365 admin center. Replace any unverified domains with the default '@yourtenant.onmicrosoft.com' if necessary.

3
Save as UTF-8 Encoded CSV

Click 'File' > 'Save As' in your spreadsheet application. Choose the 'CSV' file format and explicitly select 'UTF-8' in the encoding or tools dropdown to prevent character interpretation errors.

4
Filter Out Existing Users

Remove any user rows from the CSV that have already been imported successfully to avoid processing conflicts and duplicate account errors during the script run.

Clean and Validate CSV Data Formatting
Isolate Failing Rows: If you are importing hundreds of users, copy only the failed rows into a brand new CSV file and run the script again. This isolates the problem and often bypasses intermittent script timeouts.
Free Microsoft Office alternative

Manage Your CSV User Data Easily with WPS Office

While resolving PowerShell script errors requires administrative access to Microsoft 365, managing the underlying CSV data doesn't have to be complicated. WPS Office provides a free, lightweight, and powerful spreadsheet tool perfectly compatible with Microsoft Excel formats to help you clean up user data, fix encoding errors, and format imports seamlessly.

  1. 1. Open CSV in WPS Spreadsheet: Launch WPS Office, click on 'Open', and select your user import CSV file to view the data.
  2. 2. Clean User Data: Use the TRIM function or press 'Ctrl + H' to open the Find and Replace dialog to eliminate accidental spaces in the UserPrincipalName column.
  3. 3. Save with Correct Encoding: Click 'Menu' > 'Save As', select the 'CSV' format, and click the 'Tools' dropdown to confirm the web encoding is set to UTF-8 before saving.
Fully compatible with Microsoft Excel formats (.xlsx, .xls, .csv).Advanced spreadsheet tools to easily find and replace hidden spaces or invalid characters in your UserPrincipalName column.Built-in UTF-8 CSV saving support to prevent encoding-related PowerShell import failures.Lightweight installation with a familiar, easy-to-use tabbed interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why does New-MsolUser throw a syntax error only for specific users?

This usually happens when specific rows in the CSV contain hidden formatting issues, trailing spaces, invalid special characters, or use an unverified domain in the UserPrincipalName column.

How do I save a CSV file in UTF-8 encoding?

Open your CSV in a spreadsheet application, select 'Save As', choose the CSV file format, and look for an encoding option (often under 'Tools' or 'Web Options') to explicitly select UTF-8 before saving.

Should I still be using the New-MsolUser cmdlet for Microsoft 365?

Microsoft strongly recommends migrating from the MSOnline module (which includes New-MsolUser) to the Microsoft Graph PowerShell SDK (New-MgUser) for managing users, as the older module is officially deprecated.

What should I do if the UserPrincipalName is correct but the import still fails?

Check for invisible characters or accidental duplicate entries that already exist in Microsoft 365. You can also try creating a brand new CSV file, pasting only the failing rows as plain text, saving with UTF-8 encoding, and running the import script again.