Fix Invalid UserPrincipalName Errors When Importing Microsoft 365 Users
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.

- 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.
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.
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.
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.
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.
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.
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.

Upgrade to Microsoft Graph PowerShell
Switch from the deprecated MSOnline module to the newer Microsoft Graph PowerShell module for better error handling.
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. Open CSV in WPS Spreadsheet: Launch WPS Office, click on 'Open', and select your user import CSV file to view the data.
- 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. 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.

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.




