How to bulk assign Office 365 licenses based on AD group membership?

The Azure Active Directory Sync tool (aka DirSync) already has been setup here to synchronize on-premises Active Directory users to Office 365.
Assigning license within Office 365 admin center portal for one user is quite easy but doing this for 500-1000 users will be a time waste.

I need some help with bulk assigning Office 365 licenses based on AD group membership. Also, not all services from the licence plan will be available to the users. Some of the users (around 20-30) already have Office 365 licence, so we need to be careful not to overwrite their existing Office 365 license.

Microsoft Online Services Sign-In Assistant and Azure AD Module already installed on my Windows 8.1 workstation. Or, I could use DirSync server if I need too.

I can Connect-MsolService with my Office 365 global admin account without any problem.

Ideally I would like to have “step by step” approach with clear explanation if possible.
Thanks for your help in advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I found this user ( Vinayak Latthe ) that did this in a script :

from this site.

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(“”) |


 $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


Open in new window

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 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office 365

From novice to tech pro — start learning today.