When doing a license assignment through the GUI, we can select 25 users at a time to do license assignment. This works if you are working with 25 users or less. But what if you have 2500 users that need a license assignment? You would need to go through GUI 100 times. Probably not the best use of your time. Luckily, we can leverage the power of PowerShell to accomplish what we need.

Here is a quick example of how we can license users in bulk:

First, we’ll need the Azure Module for Windows PowerShell. This can be located in the users and groups tab in the O365 portal, and once it’s installed, we’re ready to go.

  • Connect-MSOLService
    • Type O365 Admin Creds at the prompt
  • We’ll need our license information, obtain this via:
    • Get-MSOLAccountSKU
    • Record the appropriate SKU name for the license you want to assign, e.g. “contoso:ENTERPRISEPACK”
  • Run the following for a bulk assignment
    • (Get-MSOLUser –maxResults 2500) | ForEach {Set-MSOLUser –user $_.UserPrincipalName –UsageLocation “US”; Set-MSOLUserLicense –user $_.UserPrincipalName –AddLicenses “contoso:ENTERPRISEPACK”}

So, a few things to note about this:

  1. We need to denote a usage location for the users. I have denoted US, this is a 2 digit country code, which you can find all country codes here.
  2. This will grab the user, set usage location, and assign a license for all users. If we need to be selective, we will need to leverage some more functionality.

Let’s say I want to assign licenses via domain, well, we can include a Where filter:

  • (Get-MSOLUser –maxResults 2500) |Where {$_.UserPrincipalName –like “*@Contoso.com”} |  ForEach {Set-MSOLUser –user $_.UserPrincipalName –UsageLocation “US”; Set-MSOLUserLicense –user $_.UserPrincipalName –AddLicenses “contoso:ENTERPRISEPACK”}

This will grab all of the users who are ONLY on the contoso.com domain and assign them to US location and assign our license. We can use the Where Filter on any attribute available to Get-MSOLuser, even the location attribute (provided it’s populated with data, it’s not by default). Let’s try assigning all the users on the contoso.com domain who are ALSO in the united states.

  • (Get-MSOLUser –maxResults 2500) |Where {$_.UserPrincipalName –like “*@Contoso.com” –and $_.Country –match “United States”} |  ForEach {Set-MSOLUser –user $_.UserPrincipalName –UsageLocation “US”; Set-MSOLUserLicense –user $_.UserPrincipalName –AddLicenses “contoso:ENTERPRISEPACK”}
    One last tidbit, let’s say you have a CSV or .TXT file with the usernames of your users, and you want to use this instead. Sure!
  • Get-Content C:\Users\Victor\Office365userlist.TXT |  ForEach {Set-MSOLUser –user $_ –UsageLocation “US”; Set-MSOLUserLicense –user $_ –AddLicenses “contoso:ENTERPRISEPACK”}
    You can use a .TXT or a .CSV for this purpose, however for a TXT file you’ll need to line separate them. For a CSV, you can comma delimit or use a delimiter of your choice.
Was this article helpful?