Remove licenses from users office 365

%3CLINGO-SUB%20id%3D%22lingo-sub-2325372%22%20slang%3D%22en-US%22%3ERemove%20licenses%20from%20users%20office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2325372%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20trying%20with%20no%20success%20to%20remove%20licenses%20from%20users.%3C%2FP%3E%3CP%3EI%20tried%20script%20from%20Microsoft%20docs%20but%20it%60s%20not%20working.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22dannytveria_0-1620205538099.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278354iE519CAEBCD3CA3FE%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dannytveria_0-1620205538099.png%22%20alt%3D%22dannytveria_0-1620205538099.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EI%20created%20txt%20file%20with%20the%20user%20upn.%3C%2FP%3E%3CP%3Eafter%20I%20runned%20the%20script%20I%20getting%20the%20next%20error%3A%3C%2FP%3E%3CP%3ESet-MsolUserLicense%20%3A%20User%20Not%20Found.%20User%3A%20.%3CBR%20%2F%3EAt%20line%3A4%20char%3A1%3CBR%20%2F%3E%2B%20Set-MsolUserLicense%20-UserPrincipalName%20%24x%5B%24i%5D%20-RemoveLicenses%20%22oranim%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20OperationStopped%3A%20(%3A)%20%5BSet-MsolUserLicense%5D%2C%20MicrosoftOnlineException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.Online.Administration.Automation.UserNotFoundException%2CMicrosoft.Online.Administration.Automation.SetUserLicense%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20target%20is%20to%20remove%20from%20Students%20users%20they%20license%20from%20txt%20file%20or%20csv%20file.%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2325372%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2326513%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20licenses%20from%20users%20office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2326513%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1035868%22%20target%3D%22_blank%22%3E%40dannytveria%3C%2FA%3E%2C%3CBR%20%2F%3Esounds%20like%20this%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fcommunity.spiceworks.com%2Ftopic%2F1499908-powershell-office-365-change-multiple-user-passwords%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fcommunity.spiceworks.com%2Ftopic%2F1499908-powershell-office-365-change-multiple-user-passwords%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2329722%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20licenses%20from%20users%20office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2329722%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1045823%22%20target%3D%22_blank%22%3E%40Schnittlauch%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3CBR%20%2F%3EI%20changed%20as%20you%20suggest%20and%20still%20receive%20an%20error.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Capture.PNG%22%20style%3D%22width%3A%20735px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278655i8F785116572395E0%2Fimage-dimensions%2F735x140%3Fv%3Dv2%22%20width%3D%22735%22%20height%3D%22140%22%20role%3D%22button%22%20title%3D%22Capture.PNG%22%20alt%3D%22Capture.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%E2%80%83%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2329972%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20licenses%20from%20users%20office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2329972%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1035868%22%20target%3D%22_blank%22%3E%40dannytveria%3C%2FA%3E%2C%3CBR%20%2F%3E%24_.UserPrincipalName%20is%20null.%20Do%20you%20have%20a%20list%20of%20the%20users%2C%20you%20want%20to%20remove%20the%20licenses%3F%20(Maybe%20they%20are%20all%20in%20the%20same%20AD%20Group%2C%20idk.)%3CBR%20%2F%3EIf%20yes%2C%20than%20go%20and%20make%20their%20own%20variable%20(%24Students%20for%20example)%20to%20remove%20ther%20licenses%20instead%20of%20%24_.UserPrincipalName.%3C%2FP%3E%3CP%3EIm%20wondering%20why%20the%20variable%20is%20null.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2330058%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20licenses%20from%20users%20office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2330058%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1035868%22%20target%3D%22_blank%22%3E%40dannytveria%3C%2FA%3E%26nbsp%3BOtherwise%20you%20can%20try%20it%20with%20an%20foreach%20loop%20something%20looking%20like%20this%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24UserCSV%20%3D%20import-csv%20-path%20C%3A%5CUsers%5CSchnittlauch%5CAllUsersInACsvFile.csv%3CBR%20%2F%3Eforeach%20(%24user%20in%20%24UserCSV.ColumnWithTheUsers)%3CBR%20%2F%3E%7B%3CBR%20%2F%3ESet-MsolUserLicense%20-UserprincipalName%20%24user%20-removeLicenses%20%22oranimlive%3ASTANDARDWOFFPACK_IW_STUDENT%22%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2330093%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20licenses%20from%20users%20office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2330093%22%20slang%3D%22en-US%22%3EI%20had%20the%20same%20problem%20during%20a%20long%20time%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2330188%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20licenses%20from%20users%20office%20365%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2330188%22%20slang%3D%22en-US%22%3Ehow%20u%20solved%20it%3F%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi,

I trying with no success to remove licenses from users.

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

dannytveria_0-1620205538099.png

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.

Thanks.

 

 

8 Replies

@Schnittlauch 

Hi,
I changed as you suggest and still receive an error.

Capture.PNG

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:

Connect-AzureAD
Connect-MsolService
[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. :)