Assign O365 license by PowerShell

Assigning license to a group of O365 users are always the most headache task for administrators. So why don’t we script it? Let’s start!

First of all, check the license plan and usage of your company.

Connect-AzureAD #Connect to the tenant

Get-AzureADSubscribedSku | Select SkuPartNumber #Show account service plan and grep the SkuPartNumber (e.g, Microsoft 365 E3 = ENTERPRISEPACK, Microsoft 365 F1 = SPE_F1)

#Check SKU Service Plan detail option from the above output. Let's say if there are 22 SkuPartNumber in your service plan, you would like to select the 19th one, fill in 18 in the array of $license
$licenses = Get-AzureADSubscribedSku
$licenses[18].ServicePlans

Copy the output, so now you got all of the information of your account license that you want to add. The next step is define the service plan you would like to activate for the group of users. Create a csv file, define the license option (The output above); Friendly Name; Add License Option (True or False). Below are the example, we would like to enable Microsoft Search, Skype for Business, Office Online and Exchange Online but disable Microsoft Teams.

LicenseOption;FriendlyName;addLicenseOption
MICROSOFT_SEARCH;Microsoft Search;yes
TEAMS1;Microsoft Teams;no
MCOIMP;Skype for Business Online;yes
SHAREPOINTWAC;Office Online;yes
EXCHANGE_S_DESKLESS;Exchange Online Kiosk;yes

Create another csv and import the SMTP addresses that you would like to add the licenses.

smtpAddress
[email protected]
[email protected]

Finally, the main PowerShell script.

# Powershell for license assignment                                                                                #
# Author: Wallace Ho                                                                                                     #
# Version: 1.0                                                                                                           #


$users = Import-Csv "C:\Users\wallaceho\Desktop\Licenses\users.csv" -Delimiter ";" # Define SMTP address CSV file path
$additionalOptions = Import-Csv "C:\Users\wallaceho\Desktop\Licenses\licenseplan.csv" -Delimiter ";" | ?{$_.addLicenseOption -eq "no"} #License path filter for disable option

foreach ($user in $users) #for each user in the SMTP address CSV file path
{
    $opts = New-MsolLicenseOptions -AccountSkuId "<accountid:serviceplan>" –DisabledPlans $additionalOptions.LicenseOption #Define disable option, remember to change the <accountid:serviceplan>
    Set-MsolUserLicense -UserPrincipalName $user.smtpAddress -RemoveLicenses <accountid:serviceplan>  #Remove old license, remember to change the <accountid:serviceplan>
    Set-MsolUserLicense -UserPrincipalName $user.smtpAddress -AddLicenses <accountid:serviceplan> -LicenseOptions $opts #Add new license, remember to change the <accountid:serviceplan>
}

Leave a Reply

Your email address will not be published.