Set-MsolUserLicense -RemoveLicense Issue

I am trying to an-assign a bulk of user’s licenses from our Office365.

I have all of the user’s UserPrincipalName on a CSV file like so:

That is what I tried to do:

$csv1 = import-csv | select -expandProperty name
Set-MsolUserLicense -UserPrincipalName $csv1 -RemoveLicense “domain:STANDARDPACK”

When I had only ONE user on the CSV file, IT WORKED! But when I tried to add more users to the exact file and in the exact same list, the command fails and give me this error:

Set-MsolUserLicense : Unable to assign this license because it is invalid. Use the Get-MsolAccountSku cmdlet to retrieve a list of valid licenses.

I doubled checked and made sure it is indeed the correct license (domain:StandartPack)

What else can I do? How can I make it work?


You are not looping through the user list in the CSV file.
$csv1 is not a use name, it’s a lot of user names and you can only pass one name at a time.

# Collect all the user names to provision
$csv1 = import-csv | select -expandProperty name

# step through each user and take actions needed.
ForEach($MSOUser in $csv1)
    # your code here'

There is a script on the MS that does this. See if that can replace the effort you are trying.

Bulk license assignment to Office 365 users based on CSV

If you have directory synchronization in place, your identities get provisioned in Office 365 without a license. You can use this script to bulk-add the licenses to the users based on a CSV file.

There are other approaches, depending on how you need to do this.

How to Use PowerShell to Automatically Assign Licenses to Your Office 365 Users ''

Bulk assign custom Office 365 licenses via Powershell

Bulk Assign Licenses in Office 365 Using PowerShell

Assign licenses to user accounts with Office 365 PowerShell