This article will show how to assign Microsoft 365 licenses to users using the Graph API PowerShell module.
Licensing Microsoft 365 users using automation can save time and effort in streamlining operations and free up time for more project work.
Graph API PowerShell allows us to manage almost every Microsoft 365 setting and configuration programmatically.
To get started with Graph API, visit our Graph API page and configure App Registration authentication using a certificate. The required App Registration permissions are Directory.ReadWrite.All
Code
The following code will assign a Microsoft 365 E3 license to a user. To find out the license SKU, license a user first using the portal and run the following cmdlet Get-MgUserLicenseDetail -UserId testaccount@domain.onmicrosoft.com | fl
Import-Module Microsoft.Graph.Users.Actions
$params = @{
addLicenses = @(
@{
skuId = "6fd2c87f-b296-42f0-b197-1e91e994b900"
}
)
removeLicenses = @(
)
}
$userid="testaccount@domain.onmicrosoft.com"
Update-MgUser -UserId $userId -usageLocation "AU"
Set-MgUserLicense -UserId $userId -BodyParameter $params