TechTips by MessageOpsLicense assignment can be anywhere from painless to downright annoying.  Doing it through the GUI, we can select 25 users at a time to do license assignment. Not bad, if you only have 25 users. But what about if we have 2500? Oh man, I wouldn’t want to go through the GUI 100 times!  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:

Office 365 License Assignment
Our Free Trial of 365 Command is Designed to Quickly Enable You to Manage Microsoft Office 365 Online Services Better. Get faster Office 365 management tools, analytics, reports, security & more.


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

  1. Connect-MSOLService
    1. Type O365 Admin Creds at the prompt
  2. We’ll need our license information, obtain this via:
    1. Get-MSOLAccountSKU
    2. Record the appropriate SKU name for the license you want to assign, e.g. “contoso:ENTERPRISEPACK”
  3. Run the following for a bulk assignment
    1. (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 “*”} |  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 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 domain who are ALSO in the united states.

  1. (Get-MSOLUser –maxResults 2500) |Where {$_.UserPrincipalName –like “*” –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!
  2. Get-Content C:\Users\Victor\Office365userlist.TXT |  ForEach {Set-MSOLUser –user $_ –UsageLocation “US”; Set-MSOLUserLicense –user $_ –AddLicenses “contoso:ENTERPRISEPACK”}
    1. 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.


    We hope you found this week’s Tech Tip by MessageOps on License Assignment useful.  Got a problem you want us to solve in our Tech Tip of the Week series?  Let us know.