Office 365 - Removing assigned License from Shared mailboxes using CSV

I am trying to remove Licenses from Shared Mailboxes using the following script:


$Users = Import-Csv "C:\mailbox.csv"

$Users | %{

 Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName  -RemoveLicenses $AccountSkuId


I get the following 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 absolutely know that the sku is correct.  I also closed and re-ran the PS session connecting to Exchange Online.  Any thoughts?
Anthony K O365Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vasil Michev (MVP)Commented:
You will get this error if the account in question simply does not have the SKU assigned. If you are only interested in doing this for shared mailboxes, you can quickly filter them out using:

Get-MsolUser | ? {$_.CloudExchangeRecipientDisplayType -eq 0}

Open in new window

or do the same for shared mailboxes with particilar license:

 Get-MsolUser | ? {$_.CloudExchangeRecipientDisplayType -eq 0 -and $_.Licenses.AccountSKUId -eq "Tenant:STANDARDWOFFPACK_IW_FACULTY"}

Open in new window

Then, you can just iterate against the returned objects and remove the license:

 Get-MsolUser | ? {$_.CloudExchangeRecipientDisplayType -eq 0 -and $_.Licenses.AccountSKUId -eq "Tenant:STANDARDWOFFPACK_IW_FACULTY"} | % {  Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName  -RemoveLicenses $AccountSkuId } 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony K O365Author Commented:
Excellent! Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office 365

From novice to tech pro — start learning today.