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

15 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

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

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

        1. import-module MSOnline
          Connect-MSOLService

          $users = import-csv “C:\License\BP.csv” -delimiter “,”
          foreach ($user in $users)
          {
          $upn=$user.UserPrincipalName
          $SKU=$user.SKU
          Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $SKU
          Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses $SKU
          }

  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?

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

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

  5. I’ve been attempting to use your scripts to bulk license a specific license to users on our domain. Using your script I can successfully add the license to a single user, but when I attempt to bulk add the license the script errors out saying the license is not valid. This happens when I use a CSV file with about 10 users for testing, and also when I use the Get-MsolUser -All -UsageLocation ‘US’ | Set-MsolUserLicense -AddLicenses “tenantname:ENTERPRISEPACK” command. The error I’m seeing is: “Set-MsolUserLicense : The license OurTennantName:MCOMEETADV_FACULTY is not valid. To find a list of valid licenses, please call the Get-MsolAccountSku cmdlet.” I checked and rechecked the SKUs available for my domain, and I copied and pasted the SKU name “MCOMEETADV_FACULTY”, and it works for a single user. Can you tell me why it works for single users but won’t work for multiple users?

    1. The Set-MsolUserLicense will throw that error if you’re trying to add a license that’s already assigned to the user.

  6. Pingback: Office 365 to Office 365 Migration Guide - Communication Square LLC

  7. Pingback: The Ultimate Office 365 to Office 365 Migration Guide - Communication Square LLC

Leave a Comment

Your email address will not be published.