A Microsoft user can't sign in and gets an error that the account is locked.…
How to Assign Microsoft 365 licenses with PowerShell
You can manually assign licenses to single or multiple users in the Microsoft 365 admin center or Microsoft Entra admin center. If you want to assign licenses to bulk users, it’s faster to create a CSV file and use a PowerShell script. In this article, you will learn how to assign user licenses in Microsoft Entra ID and Microsoft Graph PowerShell.
Table of contents
- Microsoft 365 user licenses
- How to assign Microsoft 365 licenses in Microsoft Entra ID
- How to assign Microsoft 365 licenses with PowerShell
- Connect to Microsoft Graph PowerShell
- Get SKU ID licenses
- Get unlicensed users
- Assign Microsoft 365 licenses to a single user with Microsoft Graph PowerShell
- Assign Microsoft 365 licenses to multiple users with Microsoft Graph PowerShell
- Assign Microsoft 365 licenses to bulk users from CSV file in PowerShell
- Conclusion
Microsoft 365 user licenses
Each organization owns different Microsoft 365 products where a product may contain licenses from multiple subscriptions. To assign licenses to your users, you need to choose a Microsoft 365 product (e.g., Microsoft 365 E5 Developer).
You can assign Microsoft licenses to users in different ways:
- Microsoft 365 admin center
- Microsoft Entra admin center
- Microsoft Graph PowerShell
We will only show you how to assign licenses to users in the Microsoft Entra admin center and PowerShell. This is because the Microsoft 365 admin center has limitations where you can assign a maximum of 20 users at a time.
How to assign Microsoft 365 licenses in Microsoft Entra ID
To assign Microsoft 365 licenses in Microsoft Entra ID to a single user or multiple users in Microsoft Entra ID, follow the steps below.
Assign Microsoft 365 licenses to single user in Microsoft Entra ID
Assign Microsoft 365 licenses to a user in Microsoft Entra ID, following these steps:
- Sign in to Microsoft Entra admin center
- Expand Identity > Users > All users
- Select the user
- Click Licenses on the left pane or at the bottom
- Click Assignments
- Select licenses
- Select license options
- Click Save
Note: Before a license can be assigned to a user, you must specify the Usage location. Otherwise, the license assignment fails for the user.
- License assignments to the member (Amanda Morgan) succeeded, and the State is Active
Assign Microsoft 365 licenses to multiple users in Microsoft Entra ID
To assign Microsoft 365 licenses to bulk users, follow the below steps:
- Sign in to Microsoft Entra admin center
- Expand Identity > Show more
- Expand Billing > Licenses
- Click All products
- Select the license plan
- Click Assign
- Click Add users and groups
- Click Users
- Select the users
- Click Select
- Check the users in the list
- Click Assignment options at the top or bottom
- Check if you want to enable all the options
- Click Review + assign at the top or bottom
- Check everything
- Click Assign
- The license assignments were successfully assigned to multiple users
If you want to assign Microsoft 365 licenses to bulk users, it’s best to create groups. Learn how to Assign Microsoft 365 licenses with group-based licensing.
How to assign Microsoft 365 licenses with PowerShell
To assign Microsoft 365 licenses to a single user or multiple users with Microsoft Graph PowerShell, follow the below steps.
Connect to Microsoft Graph PowerShell
Before you start, you must Install the Microsoft Graph PowerShell module. Start Windows PowerShell as administrator and run the below command.
Install-Module Microsoft.Graph -Force
Important: Always install the latest Microsoft Graph PowerShell module version before you run a cmdlet or script to prevent errors and incorrect results.
Run the Connect-MgGraph cmdlet with the below scopes to authenticate with Microsoft Graph.
Connect-MgGraph -Scopes "User.ReadWrite.All", "Organization.Read.All"
Now that you are all set, you can use the commands with MS Graph PowerShell.
Get SKU ID licenses
To assign licenses to multiple users with Microsoft Graph PowerShell, you need to know the SKU ID of the license. We will use the Get-MgSubscribedSku PowerShell cmdlet to get the SkuPartNumber that shows the available licensing plans for your organization. For example, ENTERPRISEPACK is the license plan name for Office 365 Enterprise E3.
Run the below PowerShell command to get the SKU ID of all the licenses in your organization.
Get-MgSubscribedSku | Select-Object SkuPartNumber, SkuId
The PowerShell output results show a list of the SKU ID licenses.
SkuPartNumber SkuId
------------- -----
DEVELOPERPACK_E5 c42b9cae-ea4f-4ab7-9717-81576235ccac
Get unlicensed users
To find all the unlicensed user accounts in your organization, you need to use the Get-MgUser cmdlet.
Run the below PowerShell command to get all unlicensed users, excluding guests.
Get-MgUser -All -Filter "assignedLicenses/`$count eq 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable unlicensedUserCount
It will show a list of all the unlicensed user accounts.
Assign Microsoft 365 licenses to a single user with Microsoft Graph PowerShell
To assign licenses to a specific user, you will use the Set-MgUserLicense PowerShell cmdlet. In our example, we want to assign the user Amanda Morgan the DEVELOPERPACK_E5 license.
Note: You must add the -RemoveLicenses parameter with an empty array @() even if you don’t want to remove any licenses.
- Specify the license SKU ID in line 1
- Specify the UserPrincipalName in line 2
- Run the below PowerShell script
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }
$user = Get-MgUser | Where-Object { $_.UserPrincipalName -eq "Amanda.Morgan@m365info.com" }
Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = ($license.SkuId) } -RemoveLicenses @()
The PowerShell output shows that is assigned the licenses to this user.
DisplayName Id Mail UserPrincipalName
----------- -- ---- -----------------
Amanda Morgan f8261d51-3df9-4f21-a6b1-533412669c11 Amanda.Morgan@m365info.com Amanda.Morgan@m365info.com
Assign Microsoft 365 licenses to multiple users with Microsoft Graph PowerShell
To assign Microsoft 365 licenses to multiple users, use the below PowerShell script:
- Specify the UserPrincipalName of the users in line 2
- Specify the license SKU ID in line 5
- Run the below PowerShell script
$users = @(
"amanda.hansen@m365info.com",
"amanda.morgan@m365info.com"
)
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }
foreach ($user in $users) {
$user = Get-MgUser -Filter "UserPrincipalName eq '$user'"
try {
$userLicense = Get-MgUserLicenseDetail -UserId $user.Id | Where-Object { $_.SkuId -eq $license.SkuId }
if ($userLicense) {
Write-Host "License $($license.SkuPartNumber) is already assigned to $($user.DisplayName)" -ForegroundColor Yellow
}
else {
Write-Host "Assign license $($license.SkuPartNumber) to $($user.DisplayName)" -ForegroundColor Green
$null = Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = ($license.SkuId) } -RemoveLicenses @() -ErrorAction Stop
}
}
catch {
Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
break
}
}
The PowerShell output shows when:
- The license is already assigned to a user
- Assign a license to the user
The PowerShell script will stop when:
- The user has no Usage Location value
- There are no more available licenses (the maximum limit is reached)
- The user does not exist or is misspelled
It shows the below PowerShell result example.
Assign license DEVELOPERPACK_E5 to Amanda Hansen
License DEVELOPERPACK_E5 is already assigned to Amanda Morgan
You can also specify a group of users with the Get-MgUser cmdlet, as seen in the example below.
- Specify the users in line 1
- Specify the license SKU ID in line 2
- Run the below PowerShell script
$users = Get-MgUser -All | Where-Object { $_.Mail -match "@m365info.com" }
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }
foreach ($user in $users) {
try {
$userLicense = Get-MgUserLicenseDetail -UserId $user.Id | Where-Object { $_.SkuId -eq $license.SkuId }
if ($userLicense) {
Write-Host "License $($license.SkuPartNumber) is already assigned to $($user.DisplayName)" -ForegroundColor Yellow
}
else {
Write-Host "Assign license $($license.SkuPartNumber) to $($user.DisplayName)" -ForegroundColor Green
$null = Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = ($license.SkuId) } -RemoveLicenses @() -ErrorAction Stop
}
}
catch {
Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
break
}
}
The PowerShell output shows when:
- The license is already assigned to a user
- Assign a license to the user
The PowerShell script will stop when:
- The user has no Usage Location value
- There are no more available licenses (the maximum limit is reached)
It shows the below PowerShell example.
License DEVELOPERPACK_E5 is already assigned to Amanda Hansen
License DEVELOPERPACK_E5 is already assigned to Amanda Morgan
Assign license DEVELOPERPACK_E5 to Blake Martin
Assign license DEVELOPERPACK_E5 to Carl Hawk
An error occurred: [Request_BadRequest] : Subscription with SKU c42b9cae-ea4f-4ab7-9717-81576235ccac does not have any available licenses.
Assign Microsoft 365 licenses to bulk users from CSV file in PowerShell
You can also create a CSV file with the unlicensed users in one column and use the PowerShell script to assign Microsoft 365 licenses.
To bulk assign Microsoft 365 licenses to users, follow the below steps:
- Open Microsoft Excel
- Type ID as the header of the first column
- Specify the UserPrincipalName
- Create the folder temp if you don’t have it already in the (C:) drive
- Name the file Users.csv
- Save as type CSV (Comma delimited (*.csv)
- Click Save
- Type the CSV file path in line 1
- Specify the license SKU ID in line 2
- Run the below PowerShell script
$Users = Import-Csv "C:\temp\Users.csv"
$license = Get-MgSubscribedSku | Where-Object { $_.SkuPartNumber -eq "DEVELOPERPACK_E5" }
foreach ($user in $users) {
$userId = $user.Id
$userObject = Get-MgUser -Filter "UserPrincipalName eq '$($user.Id)'"
if ($userObject) {
try {
$userLicense = Get-MgUserLicenseDetail -UserId $userObject.Id | Where-Object { $_.SkuId -eq $license.SkuId }
if ($userLicense) {
Write-Host "License $($license.SkuPartNumber) is already assigned to $($userObject.DisplayName)" -ForegroundColor Yellow
}
else {
Write-Host "Assign license $($license.SkuPartNumber) to $($userObject.DisplayName)" -ForegroundColor Green
$null = Set-MgUserLicense -UserId $userObject.Id -AddLicenses @{SkuId = ($license.SkuId) } -RemoveLicenses @() -ErrorAction Stop
}
}
catch {
Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
break
}
}
else {
Write-Host "User $userId does not exist" -ForegroundColor Red
}
}
The PowerShell output shows when:
- The license is already assigned to a user
- Assign a license to the user
- The user does not exist or is misspelled
The PowerShell script will stop when:
- The user has no Usage Location value
- There are no more available licenses (the maximum limit is reached)
Assign license DEVELOPERPACK_E5 to Anne Butler
License DEVELOPERPACK_E5 is already assigned to Anna Bell
License DEVELOPERPACK_E5 is already assigned to Frank Olsen
User Unknown@m365info.com does not exist
License DEVELOPERPACK_E5 is already assigned to Ken Walker
Assign license DEVELOPERPACK_E5 to Stephen Hunter
An error occurred: [Request_BadRequest] : License assignment cannot be done for user with invalid usage location.
That’s it!
Read more: Change Microsoft 365 primary SMTP address »
Conclusion
You learned how to assign Microsoft 365 licenses in Microsoft Graph PowerShell and Microsoft Entra ID. With both methods, it’s possible to assign licenses to single and multiple users. There are multiple variations that you can work with in PowerShell to assign Microsoft 365 licenses to bulk users.
Did you enjoy this article? You may also like Bulk import contacts to Microsoft 365 using PowerShell. Don’t forget to follow us and share this article.
This Post Has 0 Comments