Bulk Assign Licenses in Office 365 Using PowerShell

By | September 13, 2016

If you manage an Office 365 tenant, you are probably familiar with assigning licenses to provision services for users. That process is pretty straightforward for a single user.

license1

But how do you do it for a hundred or thousand people in your organization? PowerShell.

First, you will need to connect to Office 365 via PowerShell. If you haven’t done this before, follow these steps to install the prerequisites.

To connect to O365/MSOnline, use the following command:

Import-Module MSOnline
Connect-MsolService

You will be prompted for credentials – this needs to be a user with at least user management role permissions, but most operations in this module will require global admin permissions.

Next, you will need to get a list of licenses available in your tenant. This can be viewed easily in the admin portal under Billing, but is identified by the AccountSkuID in PowerShell. To generate a list of what is available and assigned, run the following command:

Get-MsolAccountSku

The results will contain your tenant name and sku and looks something like this:

license2

If you’re using E1/E3 licenses, they will have a name like “tenantname:ENTERPRISEPACK” or “tenantname:STANDARDPACK”.

Now that you know what you have available to assign, you need to determine which users will be assigned a license. This can be a difficult task, especially in larger organizations.

If you’re lucky enough to just assign all users in your tenant a license, your process will be relatively simple. Prior to assigning licenses, you must assign a location. This is a required field and is done by country. This will essentially provision the Exchange Online mailbox in the proper region and ensure that it follows all local laws, etc.

To assign the US location to a single user, you would use the following command:

Set-MsolUser user@domain.com $upn -UsageLocation US

All countries follow the 2-letter ISO code standard – a list of those can be found here.

Now, we’re using PowerShell – we want to actually bulk assign licenses and locations, not just do single users. To assign the US location to all of your tenant users, use the following command:

Get-MsolUser -All | Set-MsolUser -UsageLocation US

To verify the results, use the following command:

Get-MsolUser -All | Select DisplayName,UsageLocation

Once the location is assigned either through the admin portal or PowerShell, you can assign licenses. The following command would assign an E3 license to all users in the US only:

Get-MsolUser -All -UsageLocation ‘US’ | Set-MsolUserLicense -AddLicenses “tenantname:ENTERPRISEPACK”

There are several other properties that may be useful in narrowing down the scope of users to bulk assign licenses to. Use the following command to view only users that do not have a license assigned:

Get-MsolUser -UnlicensedUsersOnly

This command will assign licenses only to users with a specific domain name:

Get-MsolUser -All -DomainName ‘joshheffner.com’ | Set-MsolUserLicense -AddLicenses “tenantname:ENTERPRISEPACK”

A full list of properties to use with Get-MsolUser can be found here.

What if it isn’t this straightforward in your organization? You may have several countries, types of licenses, or maybe you want to assign licenses in batches. Sometimes it’s just easiest to assign both the location and license at the same time from a CSV file – this is usually the preferred method in larger organizations. This operation can be done with a simple PowerShell script (download it here):

license4

The above script references users in a CSV file containing users’ UPN, location, and license to assign. It looks like this (download it here):

license3

You will need to modify the script to use the correct path to the CSV file.

If you need to generate a list of users in your O365 tenant, including their UPN, location, and whether or not a license is currently assigned, you can use the following command:

Get-MsolUser | select-object DisplayName,UserPrincipalName,UsageLocation,IsLicensed

Your results will look similar to this:

license5

To export the same data to a CSV file, add a bit more to the end:

Get-MsolUser | select-object DisplayName,UserPrincipalName,UsageLocation,IsLicensed | export-csv C:\pathtofile\o365export.csv -notype

7 thoughts on “Bulk Assign Licenses in Office 365 Using PowerShell

  1. Michael O'Brien

    Hi There,
    This is a brilliant post and a real time saver.
    However, is there away to extend the script to include license options. I have managed to do this for a single user using “-DisabledPlans”. Using the script posted here, I amended it using “-License options” with success, and added a column to the csv. The issue is that I am not sure what syntax to put into the csv for each of the license options. Might you be able to help?
    Regards, Michael

    Reply
  2. Rick

    Will this script replace the currently assigned license for a user or does this just simply add to what they already have?

    Reply
    1. Josh Heffner Post author

      This script will actually add licences to what the user already has – it does not remove them automatically, but that can also be done by similar script.

      Reply
  3. sonnyace malibiran

    thanks for this wonderful script. However, is there a way we can add a log file to identify which user has successfully provided a license and whose user got failed?

    Reply
    1. Josh Heffner Post author

      Sonnyace,

      You can add a line at the end of the foreach loop like”get-msoluserlicense -userprincipalname $upn > C:\license.log” to output the newly assigned license. That may be the easiest way besides running a get-msoluser -all at the end to see the list of licenses.

      Reply
  4. azhar

    Have you been able to lookup a powershell command we can use in Azure AD to query all users who do NOT have this field defined as United States and a way to apply these settings in bulk

    Reply

Leave a Reply

Your email address will not be published.