License & Subscription Issues

Fix the PowerShell UserPrincipalName Null Error in Microsoft 365 License Removal

Huda QurayshiHuda Qurayshi Nov 25, 2026 868 views

Question details

Identify why imported user principal names are null during bulk license removal and prepare valid input for the removal command.

Fix the PowerShell UserPrincipalName Null Error When Removing Microsoft 365 Licenses
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.
Before you start

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.

Solution 1Recommended

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.

1
Inspect the input file

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.

2
Import the matching format

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.

3
Check for blank entries

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.

4
Verify the pipeline objects

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.

Check the input format and UserPrincipalName values
Use a supported module: The MSOnline module and Set-MsolUserLicense are retired. Use Microsoft Graph PowerShell for current license management rather than relying on the legacy command.
Free Microsoft Office alternative

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.

Review user and license lists in WPS SpreadsheetsCompatible with Microsoft Excel XLS and XLSX filesOpen CSV files to inspect user principal name columnsFamiliar interface for a smoother transition from Microsoft Office
microsoft office alternative - wps office

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.