Office 365 - Removing assigned License from Shared mailboxes using CSV

Anthony K O365
Anthony K O365 used Ask the Experts™
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?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Distinguished Expert 2018
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

Anthony K O365Messaging Consulting


Excellent! Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial