Bulk Assign Licenses in Office 365 Using PowerShell

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 ‘pagosa.ai’ | 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