Skip to content

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.

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:

  1. Sign in to Microsoft Entra admin center
  2. Expand Identity > Users > All users
  3. Select the user
  1. Click Licenses on the left pane or at the bottom
  1. Click Assignments
  1. Select licenses
  2. Select license options
  1. 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.

  1. 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:

  1. Sign in to Microsoft Entra admin center
  2. Expand Identity > Show more
  1. Expand Billing > Licenses
  2. Click All products
  3. Select the license plan
  1. Click Assign
  1. Click Add users and groups
  1. Click Users
  2. Select the users
  3. Click Select
  1. Check the users in the list
  2. Click Assignment options at the top or bottom
  1. Check if you want to enable all the options
  2. Click Review + assign at the top or bottom
  1. Check everything
  2. Click Assign
  1. 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.

  1. Specify the license SKU ID in line 1
  2. Specify the UserPrincipalName in line 2
  3. 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:

  1. Specify the UserPrincipalName of the users in line 2
  2. Specify the license SKU ID in line 5
  3. 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.

  1. Specify the users in line 1
  2. Specify the license SKU ID in line 2
  3. 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:

  1. Open Microsoft Excel
  2. Type ID as the header of the first column
  3. Specify the UserPrincipalName
How to assign user licenses with PowerShell CSV
  1. Create the folder temp if you don’t have it already in the (C:) drive
  2. Name the file Users.csv
  3. Save as type CSV (Comma delimited (*.csv)
  4. Click Save
How to assign user licenses with PowerShell CSV file
  1. Type the CSV file path in line 1
  2. Specify the license SKU ID in line 2
  3. 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.

o365info Team

o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *