Some PowerShell magic with Office 365 licenses

PowerShell is a great tool when we have to manage a lot of resources, because it enables automation. Most of you may be already aware that we can use PowerShell also with Office 365. So let’s see some PowerShell magic with Office 365 licenses.

These days one of my colleague had to deal with a very interesting scenario. A customer moved to Exchange Online and therefore assigned only Exchange Online licenses to the users that were synchronized from the local Active Directory. After the whole migration process was ended, the customer decided to also use the powerful features of SharepointOnline. So he wanted an automated option to assign to some users also the SharePoint Online licenses, without removing the Exchange Online license first. (Removing an Exchange Online license would disconnect the mailbox and put it into a soft deleted state. Assigning the license back would re-connect the mailbox, but in some cases this process causes some big problems). 

So my colleagues proceeded as per the official documentation: they created an array where they put all the service plans that they didn’t want (like Lync, RMS and so on), they created a variable as License Option and finally they ran the PowerShell cmdlet documented for license assignment:

Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses subscription:ENTERPRISEPACK -LicenseOption $AddSPO

Unfortunately, this resulted in a bed error message:

Capture2

 

This error is a little bit misleading. The real reason why this cmdlet fails, resides in the fact that the user already has an AccountSKUID assigned. So the problem here is with the -AddLicense parameter.

Now I feel like a magician and would like to perform a PowerShell trick. And the best way to begin a magical trick is by asking a question: do I really need the -AddLicense parameter? (Drumbeat….) The answer is NO.

So in my test environment I ran:

Set-MsolUserLicense -UserPrincipalName john@danpatr10.onmicrosoft.com -LicenseOptions $LicensePlan4

Please notice: without -AddLicense

I didn’t receive any error. But did this really work? Was the Sharepoint Online license added? Let’s check this out!

(Get-Msoluser -UserPrincipalName jane@danpatr10.onmicrosoft.com).licenses[0].ServiceStatus

Capture3

So, yes, this worked perfectly.

However, for those who would like to have a step by step instruction on how to add specific license plan to a user that already has a license assigned, here are the steps.

1. Check original state

PS C:\Users\danpatr> (Get-Msoluser -UserPrincipalName jane@danpatr10.onmicrosoft.com).licenses[0].ServiceStatus

Capture4

 

2. Create an array containing the plans that you want to keep disabled (in my case all other plans except Exchange and Sharepoint)

$SPOandEXO = @()
$SPOandEXO += “INTUNE_O365”
$SPOandEXO += “YAMMER_ENTERPRISE”
$SPOandEXO += “RMS_S_ENTERPRISE”
$SPOandEXO += “OFFICESUBSCRIPTION”
$SPOandEXO += “MCOSTANDARD”

3. Create a License plan and specify the array of plans that you want disabled

$LicensePlan3 = New-MsolLicenseOptions -AccountSkuId danpatr10:ENTERPRISEPACK -DisabledPlans $SPOandEXO

4. Assign the license plans without the -AddLicense parameter

Set-MsolUserLicense -UserPrincipalName jane@danpatr10.onmicrosoft.com -LicenseOptions $LicensePlan3

5. Check the result

(Get-Msoluser -UserPrincipalName jane@danpatr10.onmicrosoft.com).licenses[0].ServiceStatus

Everything should be fine. Please wait few minutes between step 4 and 5 since provisioning the service could take few seconds.

This is the core scenario. Of course you can add to this also a lot of programming logic and create a script which may extract users from a CSV and perform this action in any way you want based on an if-else logic.

Hope this helps you since I didn’t find any official Microsoft documentation saying that we can run Set-MsolUserLicense also without the -AddLicense parameter.

Dan Patrascu-Baba

Partner Technical Consultant at Microsoft
Azure PaaS and dev consultant, working for Microsoft. Mostly dealing with Microsoft Azure services, ASP.Net Core, AngularJS, Javascript. Helping partners and customers to write good code and to architect their cloud and hybrid solutions.

3 thoughts on “Some PowerShell magic with Office 365 licenses

  1. David Chadwick

    Hey would it be possible to bulk something like this?

    Import-Csv “C:\licensing\license_users.CSV” | ForEach {Set-MsolUserLicense -UserPrincipalName $_.UPN -LicenseOptions $LicensePlan3}”
    I tried the following however it errored out

  2. Dan Patrascu-Baba Post author

    Thank you for your comment. Just few days back I was working with a partner and created a brief script to read a CSV file and assign to certain users some licenses. And it worked perfectly.

    Can you please share the error message?
    At a first sight, I don’t know what the variable $LicensePlan3 contains, but if the users in the csv are fresh users, you may also need to assign the SKU with -AddLicense contoso:ENTERPRISEPACK.

  3. Pingback: Azure AD Connect default synchronization interval and manual sync process have totally changed - danpatrascu.com

Leave a Reply

Your email address will not be published. Required fields are marked *