How to assign licenses to users on Office 365 using Powershell

Whether users are provisioned via Azure AD Connect Sync or directly on office 365 cloud (In-Cloud Users). We need to assign the licenses to users to get certain features and functionality. For working with Exchange Online, Once you have sync’ed the user to office365, you can assign appropriate license (e.g. Office 365 E3 or office 365 Business Premium etc.) depending upon the plan you purchased from Microsoft to get their mailbox created in Exchange Online.

Read More: Powershell – Export the list of users and assigned licenses from Microsoft 365

How to assign Office 365 Licenses using Powershell

Please follow below steps to assign an Office 365 / Microsoft 365 license to the user:

  1. Connect to Exchange Online (EXO)
$mycred = Get-Credential <Global Administrator User ID>
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $mycred -Authentication Basic -AllowRedirection
Import-PSSession $Session
  1. Connect to Azure Active Directory (Azure AD)
  1. Capture the UserPrincipalname of all the users in a variable or export it to a file called userprincipalname.txt. I will recommend to export it to a txt file and filter the list to see which users needs to be assigned licenses.
$users = Get-msoluser | Select Userprincipalname


Get-msoluser | select Userprincipalname | out-file c:\temp\userprincipalname.txt

The userprincipalname.txt file should only contain the UPNs e.g. as shown below


  1. Find out which license needs to be assigned to the user using link Microsoft Licensing Service Plan Reference

For example: AAD_PREMIUM (Azure Active Directory Premium P1) therefore I used AAD_Premium in the below script to add the license to the users. I will recommend to run the command on one or two users first and check it on office365 if the license has been assigned successfully. Once its successful, you can just add the UPNs of the users in a text file and run it using foreach loop in powershell using below code.

  1. Assign /Add  AAD_PREMIUM license to one user 
Set-MsolUserLicense -UserPrincipalName -AddLicenses "AAD_PREMIUM"
  1. Assign/Add AAD_PREMIUM License to list of users mentioned in text file called userprincipalname.txt
$users = Get-Content c:\temp\userprincipalname.txt
foreach($user in $users)
Write-host "Setting AAD Premium on '$user'" -ForegroundColor Black -BackgroundColor Green
Set-MsolUserLicense -UserPrincipalName $user -AddLicenses "AAD_PREMIUM"

How to assign Office 365 License from Microsoft 365 admin center

You can follow below steps to assign Office 365 license to the users:

  1. Login on Microsoft 365 admin center
  2. Click on Users -> Active Users
  3. Click on any user -> Click on Licenses and apps.
  4. Use the checkbox next to the license to assign a license.


In this blog post, we have seen how to add / assign an office 365 license to users by using Microsoft 365 admin center and also using Powershell.

Leave a Comment