Bulk assign Office 365 Licenses to users

I recently had Partner wanted to Bulk assign Office 365 Licenses to DirSync users. In that regard I started looking around for some PowerShell scripts that would allow me to do this (Although Office 365 portal helps assign license in bulk). Users were already populated on Office 365 using DirSync.

So after much trial and error I have come up with a Script you can run against a email address list CSV dump that will assign the license for you in bulk. Some of the steps are well known, like connecting PowerShell remotely to Office 365.

1.Copy your user email list to a CSV text file and set the header for the one column to UserPrincipalName and put the users email address under it one line per user like the example below.


2.Install Microsoft Online Services Sign-In Assistant


3.Install Azure AD Module


4.Find the Azure ADPowerShell Icon and right click then Run As Administrator

5.Go to your C:\ and create a folder called Scripts

Create a new text file and copy the following code into it, then save the file as licenses.ps1 and save it to the scripts folder.



#CSV file picker module start

Function Get-FileName($initialDirectory)


 [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |


 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog

 $OpenFileDialog.initialDirectory = $initialDirectory

 $OpenFileDialog.filter = “All files (*.*)| *.*”

 $OpenFileDialog.ShowDialog() | Out-Null


#CSV file picker module end

#Variable that holds CSV file location from file picker

$path = Get-FileName -initialDirectory “c:\”

#Window with list of available 365 licenses and their names

Get-MsolAccountSku | out-gridview

#Input window where you provide the license package’s name

$server = read-host ‘Provide licensename (AccountSkuId)’

 #CSV import command and mailbox creation loop

import-csv $path | foreach {

Set-MsolUser -UserPrincipalName $_.UserPrincipalName -usagelocation “US”

Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses “$server”


#Result report on licenses assigned to imported users

import-csv $path | Get-MSOLUser | out-gridview


In Azure PowerShell window and set the execution policy to unrestricted by running the following command in the PowerShell window

Set-ExecutionPolicy RemoteSigned
Hit “Y” when prompted
Run the script - .\licenses.ps1
First prompt is for your Office 365 admin credentials. Use the admin@domain.onmicrosoft.com account that was created the first time you setup Office 365.
Second Prompt is for the location of the UserPrincipalName CSV file, just browse to where you saved it (c:\scripts) and select it then hit ok.
Third window will look up the sku of the licenses you purchased. Copy and paste the name into the next window and hit ok.
Then the script will run and assign the licenses to the users.

I hope this helps you with your Office 365 migrations.

Comments (4)

  1. Mike says:

    When i use this script, it never prompts me the 2nd time for the CSV file

    any ideas?

  2. Abdullah Salam says:

    Hi iVinayak,

    I worked like a charm. Thank you so much…

  3. Need sample csv file to upload

  4. Steve says:

    This was a life saver. A couple of small tweaks to fit our license scenario, but worked like a charm. Thank you for making this available!

Skip to main content