Remove licenses from users office 365

Brass Contributor


I trying with no success to remove licenses from users.

I tried script from Microsoft docs but it`s not working.


I created txt file with the user upn.

after I runned the script I getting the next error:

Set-MsolUserLicense : User Not Found. User: .
At line:4 char:1
+ Set-MsolUserLicense -UserPrincipalName $x[$i] -RemoveLicenses "oranim ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.SetUserLicense


My target is to remove from Students users they license from txt file or csv file.




8 Replies


I changed as you suggest and still receive an error.


Hi @dannytveria,
$_.UserPrincipalName is null. Do you have a list of the users, you want to remove the licenses? (Maybe they are all in the same AD Group, idk.)
If yes, than go and make their own variable ($Students for example) to remove ther licenses instead of $_.UserPrincipalName.

Im wondering why the variable is null.

@dannytveria Otherwise you can try it with an foreach loop something looking like this


$UserCSV = import-csv -path C:\Users\Schnittlauch\AllUsersInACsvFile.csv
foreach ($user in $UserCSV.ColumnWithTheUsers)
Set-MsolUserLicense -UserprincipalName $user -removeLicenses "oranimlive:STANDARDWOFFPACK_IW_STUDENT"

I had the same problem during a long time
how u solved it?
I used the same script but change some arguments:

[string[]]$x=Get-Content "D:\Logs\O365\Students with all licesnse\Bulk\Remove license test.txt"
for ($i=0; $i -lt $x.Count; $i++)
Set-MsolUserLicense -UserPrincipalName $x[$i] -RemoveLicenses "License SKU"

Good job @dannytveria!
Is it working? If yes than close the topic, for upcomming people with the same problem. :)