Allocate licenses in bulk to your Office 365 users (Option 1 : Using PowerShell)

As an Office 365 IT professional, you may need to allocate license to users. There are several ways for doing that. Here, I will show you two ways to achieve your goal.

  • Option 1 : Allocate licenses in bulk to your Office 365 users using PowerShell
  • Option 2 : Allocate licenses in bulk to your Office 365 users using Azure Active Directory group membership

Option 1 : Allocate licenses in bulk to your Office 365 users using PowerShell

Below is the script I’m sharing with you.

Function Set-o365UsersLicense{

  [CmdletBinding()]
  Param(
    [Parameter(mandatory=$true)]$upnPath,                                 #Path to the .csv file containing users UserPrincipalName
    [Parameter(mandatory=$false)]$LicensePlan = "E1",                     #Office 365 License Plan to be allocated to users
    [Parameter(mandatory=$false)]$DisabledOptions = "YAMMER_ENTERPRISE",  #Disabled Feature
    [Parameter(mandatory=$true)]$LogPath = "c:\"                          #Logs path
  )

  $Error.Clear()
  $LicenseToAdd = $Null
  $DisableYammer = $False
  $LicenseOptions = @()

  #Finding License Plan to activate
  Switch($LicensePlan){
    "E1" { 
        Write-host "You chosed Plan E1" -ForegroundColor Green
        $LicenseToAdd = "kingo:STANDARDPACK" 
        $LicenseToRemove = "kingo:ENTERPRISEPACK","kingo:WACONEDRIVESTANDARD" 
    }
   "E3" {
        Write-host "You chosed Plan E3" -ForegroundColor Green
        $LicenseToAdd = "kingo:ENTERPRISEPACK" 
        $LicenseToRemove = "kingo:STANDARDPACK","kingo:WACONEDRIVESTANDARD" 
    }
   "OD" {
        Write-host "You chosed To assign OneDrive" -ForegroundColor Green
        $LicenseToAdd = "kingo:WACONEDRIVESTANDARD" 
        $LicenseToRemove = "kingo:STANDARDPACK","kingo:ENTERPRISEPACK" 
    }
    Default { 
        Write-Host "$LicensePlan is not a valid License Plan parameter !" -ForegroundColor Red
    }
  }

  #Importing users to be assigned License
  $upns = Import-Csv $upnPath

  #Checking Licenses availability
  $MsolAccountSku = Get-MsolAccountSku | ? {$_.AccountSkuId -ilike $LicenseToAdd}
  $AvailableLics = $MsolAccountSku.ActiveUnits - $MsolAccountSku.ConsumedUnits

  #Assigning License to user
  If($AvailableLics -ge $upns.Count){

    #Creating not existing upns log file
    "Not existing upn List" > $($LogPath + "Upns_Not_Exist.log.log")

    #Defining License options to disable
    if($DisabledOptions -in $MsolAccountSku.ServiceStatus.ServicePlan.ServiceName){
      $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $LicenseToAdd -DisabledPlans $DisabledOptions
    }

    #Enabling users Licenses
    $upns | % {

      #Checking if user exists before processing with licensing
      $_.UserPrincipalName
      If($User = Get-MsolUser -UserPrincipalName $_.UserPrincipalName -ErrorAction SilentlyContinue){

        #Getting current user Licenses
        $CurrentUserLicenses = $User.Licenses.AccountSkuId

        #Removing unecessary Licenses
        $LicenseToRemove | %{
          if($CurrentUserLicenses -contains $_){ 
            Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -RemoveLicenses $_
          }
        }

        #Assigning New License to user if not already enabled
        If($CurrentUserLicenses -notcontains $LicenseToAdd){

          #Disable YAMMER if License to add is plan E1, E3
          If($LicenseOptions){
            Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -AddLicenses $LicenseToAdd -LicenseOptions $LicenseOptions -EA SilentlyContinue
          }
          Else{
            Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -AddLicenses $LicenseToAdd
          }
        }
      }
      Else{
        #Logging inexisting user account
        $User.UserPrincipalName >> $($LogPath + "Upns_Not_Exist.log")
      }
    }
  }
  Else{
    Write-Host "There are $AvailableLics $LicensePlan Licenses available" -ForegroundColor Red
    Write-Host "Please reduce users list to match available Licenses or Increase available Licenses" -ForegroundColor Red
  }

  #Exporting errors log
  $Error > $($LogPath + "Set_AzureUserLicense_Error.log")
  Write-Host "Script Logs Location : $LogPath " -ForegroundColor Yellow
}

A little description about the script parameters is necessary :

$upnPath : is the full path of the .csv file containing users upns

$LicensePlan : specify the license plan you want to configure

  • E1 : Office 365 Enterprise E1
  • E3 : Office 365 Enterprise E3
  • E5 : Office 365 Enterprise E5
  • OD : OneDrive Entreprise (Plan 1)

$DisabledOptions : specify the license feature you want to disable.  “YAMMER_ENTERPRISE” : designate Yammer as the feature to disable

$LogPath = designate the directory path where to save log files

“kingo” : refer to the name of my Office 365 tenant : kingo.onmicrosoft.com

The upns (UserPrincipalName) csv file must be formated like below :

UserPrincipalName
user1@domain.com
user2@domain.com
user2@domain.com
user2@domain.com

Prerequisites for running the script :

  • You will need Office 365 Module For PowerShell
  • Connect to your Office 365 tenant using the command : Connect-MsolService

How to run the script ?

  • With default parameters 
Set-o365UsersLicense -upnPath c:\upns.csv  -LogPath "d:\"

Users will be allocated License E1 with YAMMER disabled, and the log file saved in d:\ path.

  • With full parameters 

If the default parameters does not suit you, you can run a complete command by specifying full parameters like below :

Set-o365UsersLicense -upnPath "c:\upns.csv" -LicensePlan E3 -DisabledOptions "YAMMER_ENTERPRISE" -LogPath "d:\"

To check that users are well assigned licenses, use my previous post Show-LicensesMatrix.

Et voilà… 🙂

Option 2 : Allocate licenses in bulk to your Office 365 users using Azure Active Directory group membership  (coming soon…)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: