Fix the PowerShell UserPrincipalName Null Error in Microsoft 365 License Removal
Question details
Identify why imported user principal names are null during bulk license removal and prepare valid input for the removal command.

- Product
- Microsoft 365 and PowerShell
- Device & OS
- not provided
- Scenario
- An administrator imports user data from a text file to remove Microsoft 365 licenses in bulk.
- Observed behavior
- Set-MsolUserLicense fails because the UserPrincipalName value passed to it is null.
Check whether the input is a plain text file with one user principal name per line or a CSV file with a UserPrincipalName column. Confirm that you have permission to manage licenses and know which license SKU you intend to remove.
Check the input format and UserPrincipalName values
Use this check to find blank values or a mismatch between the file format and the property used by the script.
Open the file in a text editor. For a plain text file, put one user principal name, such as user@example.com, on each line. For a CSV file, use the column header UserPrincipalName and put one user principal name in each row.
In PowerShell, use $users = Get-Content '.\users.txt' for a plain text file. For a CSV file, use $users = Import-Csv '.\users.csv' and access each value through the UserPrincipalName property.
For a text file, run $users | Where-Object { [string]::IsNullOrWhiteSpace($_) }. For a CSV file, run $users | Where-Object { [string]::IsNullOrWhiteSpace($_.UserPrincipalName) }. Correct or remove any returned rows in the source file, then import it again.
Run $users | Select-Object -First 5 to inspect the imported data. Text-file entries are strings, so use $_ directly; CSV entries are objects, so use $_.UserPrincipalName. Verify that the values identify the intended users before running a license removal command.

Remove direct licenses with Microsoft Graph PowerShell
Use Microsoft Graph PowerShell after validating the user list and identifying the license SKU.
Review Microsoft 365 License Lists with WPS Office
WPS Office is a free, lightweight, easy-to-use alternative for working with office documents. Its familiar interface and Microsoft Office format compatibility make it easy to move existing files, although it does not manage Microsoft 365 licenses or fix PowerShell errors.

Frequently Asked Questions
Why is UserPrincipalName null when I import a text file?
Get-Content returns strings, not objects with a UserPrincipalName property. Use each string directly, such as $_, or import a CSV with a UserPrincipalName header and use $_.UserPrincipalName.
How can I check the column names in my CSV file?
Run Import-Csv '.\users.csv' | Select-Object -First 1 in PowerShell and inspect the displayed properties. The script's property name must match the column containing the user principal names.
What should I use instead of Set-MsolUserLicense?
Use Microsoft Graph PowerShell and Set-MgUserLicense. Connect with the required permissions, identify the license SKU with Get-MgSubscribedSku, and provide its SKU ID in the RemoveLicenses parameter.
Can I remove a group-assigned license directly from a user?
No. Change the license assignment on the group or remove the user from the group, as appropriate. A direct user-level removal command does not remove a license inherited from a group.




