Assigning or removing Microsoft 365 licenses is a common task for IT admins, but doing it with PowerShell isn't always straightforward. It requires fetching the correct SKU ID (the unique identifier for each license), handling precise syntax with hash tables, and ensuring the user has the required attributes like UsageLocation set. On the other hand, ADManager Plus, a Microsoft 365 management and reporting tool, eliminates this complexity. With a few clicks, you can assign or remove licenses for multiple users, filter by group, and skip the scripting altogether.
Before using the Set-MgUserLicense cmdlet, make sure the following requirements are met:
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All"
The Set-MgUserLicense cmdlet can be used in Microsoft Graph PowerShell to manage Microsoft 365 user licenses.
Here's the syntax:
Set-MgUserLicense
-UserId <String>
[-AddLicenses <IMicrosoftGraphAssignedLicense[]>]
[-RemoveLicenses <String[]>]
[-AdditionalProperties <Hashtable>]
[-ResponseHeadersVariable <String>]
[-Headers <IDictionary>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
The following table contains some parameters that can be used along with the Set-MgUserLicense command to manage Microsoft 365 user licenses efficiently.
| Parameter | Description |
|---|---|
| -UserId | Specifies the user (by UPN or object ID) whose license is being modified. |
| -AddLicenses | An array of license objects to assign. At a minimum, each object must include a SKU ID. |
| -RemoveLicenses | An array of license SKU IDs to remove from the user. |
| -AdditionalProperties | Passes any extra parameters supported by the API request body. |
| -ResponseHeadersVariable | Stores response headers in a variable. |
| -Headers | Custom request headers. |
| -WhatIf | Shows what would happen if the cmdlet runs. |
| -Confirm | Prompts for confirmation before execution. |