Assign licenses to user accounts with Office 365 PowerShell

Summary: Explains how to use Office 365 PowerShell assign an Office 365 license to unlicensed users.

Users can't use any Office 365 services until their account has been assigned a license from a licensing plan. You can use Office 365 PowerShell to quickly assign licenses to unlicensed accounts.

Use the Azure Active Directory PowerShell for Graph module

First, connect to your Office 365 tenant.

Next, list the license plans for your tenant with this command.

Get-AzureADSubscribedSku | Select SkuPartNumber

Next, get the sign-in name of the account to which you want add a license, also known as the user principal name (UPN).

Finally, specify the user sign-in name and license plan name and run these commands.

$userUPN="<user sign-in name (UPN)>"
$planName="<license plan name from the list of license plans>"
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$License.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $planName -EQ).SkuID
$LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$LicensesToAssign.AddLicenses = $License
Set-AzureADUserLicense -ObjectId $userUPN -AssignedLicenses $LicensesToAssign

Use the Microsoft Azure Active Directory Module for Windows PowerShell

First, connect to your Office 365 tenant.

Run the Get-MsolAccountSku command to view the available licensing plans and the number of available licenses in each plan in your organization. The number of available licenses in each plan is ActiveUnits - WarningUnits - ConsumedUnits. For more information about licensing plans, licenses, and services, see View licenses and services with Office 365 PowerShell.

To find the unlicensed accounts in your organization, run this command.

Get-MsolUser -All -UnlicensedUsersOnly

You can only assign licenses to user accounts that have the UsageLocation property set to a valid ISO 3166-1 alpha-2 country code. For example, US for the United States, and FR for France. Some Office 365 services aren't available in certain countries. For more information, see About license restrictions.

To find accounts that don't have a UsageLocation value, run this command.

Get-MsolUser -All | where {$_.UsageLocation -eq $null}

To set the UsageLocation value on an account, run this command.

Set-MsolUser -UserPrincipalName "<Account>" -UsageLocation <CountryCode>

For example:

Set-MsolUser -UserPrincipalName "belindan@litwareinc.com" -UsageLocation US

If you use the Get-MsolUser cmdlet without using the -All parameter, only the first 500 accounts are returned.

Assigning licenses to user accounts

To assign a license to a user, use the following command in Office 365 PowerShell.

Set-MsolUserLicense -UserPrincipalName "<Account>" -AddLicenses "<AccountSkuId>"

This example assigns a license from the litwareinc:ENTERPRISEPACK (Office 365 Enterprise E3) licensing plan to the unlicensed user **belindan@litwareinc.com**:

Set-MsolUserLicense -UserPrincipalName "belindan@litwareinc.com" -AddLicenses "litwareinc:ENTERPRISEPACK"

To assign a license to many unlicensed users, run this command.

Get-MsolUser -All -UnlicensedUsersOnly [<FilterableAttributes>] | ForEach {Set-MsolUserLicense -AddLicenses "<AccountSkuId>"}

Note

You can't assign multiple licenses to a user from the same licensing plan. If you don't have enough available licenses, the licenses are assigned to users in the order that they're returned by the Get-MsolUser cmdlet until the available licenses run out.

This example assigns licenses from the litwareinc:ENTERPRISEPACK (Office 365 Enterprise E3) licensing plan to all unlicensed users:

Get-MsolUser -All -UnlicensedUsersOnly | ForEach {Set-MsolUserLicense -AddLicenses "litwareinc:ENTERPRISEPACK"}

This example assigns those same licenses to unlicensed users in the Sales department in the United States:

Get-MsolUser -All -Department "Sales" -UsageLocation "US" -UnlicensedUsersOnly | ForEach {Set-MsolUserLicense -AddLicenses "litwareinc:ENTERPRISEPACK"}

New to Office 365?

Tip

LinkedIn logo New to Office 365?

Discover free video courses for Office 365 admins and IT pros, brought to you by LinkedIn Learning.

See also

Manage user accounts and licenses with Office 365 PowerShell

Manage Office 365 with Office 365 PowerShell

Getting started with Office 365 PowerShell