Assign Microsoft 365 licenses to user accounts with Microsoft Graph

Jack Chuong 436 Reputation points
2023-12-28T04:41:46.09+00:00

Hi all,

I used to switch/manage user licenses as follows

Get-MsolUser -EnabledFilter EnabledOnly -MaxResults 500 | Where-Object {($_.licenses).AccountSkuId -match "old pool"} | 
ForEach-Object { Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses "new pool" -RemoveLicenses "old pool" }

Now "Set-MsolUserLicense" is deprecated , it is required to use "Set-MgUserLicense"
But "Set-MgUserLicense" only support "userid" , not "userprincipalname"
And "Get-MgUser" not return "Licenses.AccountSkuID"
Please give me some advice, thank you very much.

Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack Chuong 436 Reputation points
    2023-12-28T06:31:35.84+00:00

    I found solution

    $removesku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'old pool'
    Get-MsolUser -EnabledFilter EnabledOnly -MaxResults 500 | Where-Object {($_.licenses).AccountSkuId -match "old pool"} | ForEach-Object { Set-MgUserLicense -UserId $_.UserPrincipalName -AddLicenses @{SkuId = "new pool"} -RemoveLicenses @($removesku.SkuId) }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Md Asif Muztaba 340 Reputation points Microsoft External Staff
    2024-01-02T00:18:11.9466667+00:00

    I understand your concern. The transition from Set-MsolUserLicense to Set-MgUserLicense requires some changes in the way you manage user licenses. Here’s a general approach to handle this:

    Get the User ID: Since Set-MgUserLicense requires the User ID instead of the User Principal Name (UPN), you’ll need to first retrieve the User ID. You can do this by using the Get-MgUser cmdlet with the UPN, and then extract the Id property.

    1. Get the License SKU ID: To get the Licenses.AccountSkuId, you can use the Get-MgUserLicenseDetail cmdlet. This cmdlet returns the license details of a specific user account, including the SKU ID.
    2. Set the User License: Once you have the User ID and the License SKU ID, you can use the Set-MgUserLicense cmdlet to add or remove licenses.

    Here’s a sample script that illustrates these steps:

    # Connect to Microsoft Graph
    Connect-Graph -Scopes User.ReadWrite.All, Organization.Read.All
    
    # Get the User ID
    $userId = (Get-MgUser -UserPrincipalName "******@domain.com").Id
    
    # Get the License SKU ID
    $licenseSkuId = (Get-MgSubscribedSku | Where-Object SkuPartNumber -eq 'YOUR_LICENSE_NAME').SkuId
    
    # Set the User License
    Set-MgUserLicense -UserId $userId -AddLicenses @{"SkuId" = $licenseSkuId} -RemoveLicenses @("old pool")
    

    Please replace "******@domain.com" and 'YOUR_LICENSE_NAME' with the actual UPN and license name you want to assign. Also, replace "old pool" with the actual old pool you want to remove.

    Remember to handle errors and edge cases according to your needs. This is just a basic example and might need adjustments based on your specific requirements.

    Please note that you need appropriate permissions to run these cmdlets. For Set-MgUserLicense, you need User.ReadWrite.All permission scope. For Get-MgUserLicenseDetail, you need User.Read.All permission scope.

    I hope this helps! Let me know if you have any other questions.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.