Powershell – Applying Licenses to list of users on Office 365

|
, | TechPress-KBID-168
Home » Office 365 » Powershell – Applying Licenses to list of users on Office 365

Summary: Whether the users are provisioned via Azure AD Connect Sync or directly on office 365 cloud (In-Cloud Users). We need to allocate and assign the licenses to the users to get certain features and functionality enabled for them. e.g. 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.

You can assign the licenses via GUI or using Powershell as shown below:

GUI Method

Assign a license using GUI is simple:

  • just Select the user on office 365 -> go to Product Licenses and click Edit.
  • Configure the users location and Toggle the Switches next to licenses to switch it ON or OFF (if you are unable to allocate the license to a user, it could be that the license has not been allocated or available)

Powershell Method

Assign a license to the users using Powershell:

  • Connect to Exchange Online (EXO)
$mycred = Get-Credential <Global Administrator User ID>
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $mycred -Authentication Basic -AllowRedirection
Import-PSSession $Session
  • Connect to Azure Active Directory (Azure AD)
connect-msolservice
  • 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

OR

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

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

Userprincipalname.txt

[email protected]
[email protected]
[email protected]

e.g. in my case it was 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. Then once you are confident, you can just copy the UPNs of the users in a text file and run it using foreach loop in powershell using below code.

  • Assign /Add  AAD_PREMIUM license to one user 
Set-MsolUserLicense -UserPrincipalName [email protected] -AddLicenses "AAD_PREMIUM"
  • 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"
}

References:

To Export the list of Office365 users with their Assigned Licenses you can follow below link:

Leave a Reply