Skip Ribbon Commands
Skip to main content

Quick Launch

Todd Klindt's home page > Todd Klindt's Office 365 Admin Blog > Posts > Getting “Unable to assign this license because it is invalid” in Office 365 PowerShell with Set-MsolUserLicense when you know darn well it’s valid
December 08
Getting “Unable to assign this license because it is invalid” in Office 365 PowerShell with Set-MsolUserLicense when you know darn well it’s valid

I’ve been working on a blog post about the script I use to license users inside of Office 365. It’s a doozy. You’re going to love it, I promise. As a rule, I have someone proof read all of my blog posts, especially ones that have PowerShell code in it. For those of you that have read any of my blog posts, that might come as a shock, but it’s true. When Shane was testing my code he kept getting errors. In this case, I just chalked it up to his ineptitude, which is the source of many of his troubles, so I blew him off. I had tested the crap out of this PowerShell script so I trust it more than I trust Shane.

But a couple of days ago I was running my trusty script and I got the error. Now stuff was getting serious! Here’s the code I ran:

PS C:\> $lic1 = "MOD873457:ENTERPRISEPREMIUM"
PS C:\> $user = "alonso@MOD873457.onmicrosoft.com"
PS C:\> Set-MsolUserLicense -UserPrincipalName $user -AddLicenses $lic1

Here’s the very unpleasant response that PowerShell gave me:

Set-MsolUserLicense : Unable to assign this license because it is invalid. Use the Get-MsolAccountSku cmdlet to retrieve a list of valid licenses.
At line:1 char:1
+ Set-MsolUserLicense -UserPrincipalName $user -AddLicenses $lic1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.InvalidUserLicenseException,Microsoft.Online.
   Administration.Automation.SetUserLicense

I did like it suggested, and sure enough the SKU was legit:

PS C:\> Get-MsolAccountSku

AccountSkuId                ActiveUnits WarningUnits ConsumedUnits
------------                ----------- ------------ -------------
MOD873457:ENTERPRISEPREMIUM 25          0            1
MOD873457:PROJECTPREMIUM    25          0            0

Here is my failure in pictures

 

SNAGHTML1d5d4728

It turns out that Shane is not an idiot. Well, if he is, this is not an example of it. It’s just a poor error message. While the error says the license is invalid, it’s really not. It threw that error because that user already had that license.

We can verify that with this command:

(Get-MsolUser -UserPrincipalName $user).licenses

The output looks like this:

image

Unfortunately there’s no good way that I know of to tell why you’re getting that error, or walk through a user’s existing licenses and see if it’s already applied. It can be done, but it’s ugly text parsing. If I figure out something elegant, I’ll let you all know.

After much swearing and figuring this out on my own, I did find one vague reference to it in Microsoft’s

Assign licenses to user accounts with Office 365 PowerShell document.

image

Not helpful, Microsoft, not helpful.

 

tk

ShortURL: http://www.toddklindt.com/InvalidO365SKU

Comments

I feel like an idiot.

My head hit the desk when I realised I'd spent fifteen minutes trying to remove licenses that had already been removed.

For reference, no text parsing, just use where-object to compare the contents of two arrays of skuids:

$SKUsToAdd = @('contoso:EXCHANGESTANDARD','contoso:O365_BUSINESS_PREMIUM')
$Users = @('joe@contoso.com','jane@contoso.com')

Foreach ($User in $Users) {
   $CurrentSKUs = (Get-MsolUser -UserPrincipalName $User).Licenses.AccountSKUID
   $NewSKUs = $SKUsToAdd | where {$CurrentSKUs -NotContains $_}

   If ($NewSKUs) {
       Set-MSOLUserLicense -UserPrincipalName $User -AddLicenses $NewSKUs
   }

}



 on 8/22/2017 1:15 AM

thank you

lot of time waste looking for the reason of that error.
 on 4/6/2018 8:14 AM

Thanks

You saved me so much time ...

Thanks
 on 1/30/2019 12:32 PM

awesome, thank you!

hit the nail right on the head
 on 2/27/2019 10:57 AM

Thanks

Thank you. Had the exact same problem. Saved me a lot of time.
 on 8/15/2019 6:22 AM

3 years later and still the same error

Microsoft still have not fixed this error (showing a wrong 'error' message).
Is is that hard to display something like "WARNING: User already has that license assigned".

Come on!
 on 12/9/2019 9:44 AM

Thank you

My test script had my own user which had the licence already to check for errors but when running with 10 users in the supporting csv it didn't error once. 

Weird, but thanks to this blog I was able to retest my script with just one existing user and I got the same error. 

Thanks again
 on 1/13/2020 11:54 AM

Re: Thank you

What a pain that this is still a problem. :(

tk
Todd O. KlindtNo presence information on 1/14/2020 8:52 AM

Maybe not elegant

Came across your blog as I was getting that error.

This may not be the neatest way, but at least it checks if the licence is already assigned before trying to assign it

$365User = Get-MsolUser -UserPrincipalName $Room -ErrorAction SilentlyContinue
            If ($365User.Licenses.AccountSKUId -notcontains 'domain:STANDARDPACK')
            {
                Set-MsolUserLicense -UserPrincipalName $Room -AddLicenses "domain:STANDARDPACK"
                Write-Host -ForegroundColor Green "E1 assigned to $Room"
            }
            Else
            {
                Write-Host -ForegroundColor Yellow "E1 already assigned"
            }
        }
 on 1/21/2020 9:35 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Today's date *

Select a date from the calendar.
Please enter today's date so I know you are a real person

Twitter


Want a message when I reply to your comment? Put your Twitter handle here.

Attachments

 

 SysKit