PowerAddict.net

powerAddict.NET

by Lou Goban
IT Application Professional at Syntax

How to remove license from a group via MS Graph API and PowerShell

First step is to connect to MS Graph API, which is described here.

Then we need two attributes:

  • Group ID (object ID)
  • License ID (sku ID)

To find out GroupID, we can use Get-MsolGroup cmdlet, or check group properties via GUI or we can use MS Graph API as shown below:

$apiUrl = 'https://graph.microsoft.com/v1.0/Groups/'
$Data = Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $apiUrl -Method Get
$Groups = ($Data | select-object Value).Value | Select-Object id,displayName

# Show the groups
$Groups

Then we need License skuId/GUID (Please check MS website for Product names and service plan identifiers for licensing or amazing blog of one of my colleague Philipp Foeckeler)

$apiUrl = 'https://graph.microsoft.com/v1.0/Groups/<GUID>?$select=assignedLicenses'
$Data = Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $apiUrl -Method Get
$GroupData = $Data | select-object -ExpandProperty assignedLicenses


$GroupData | fl

Output of the code:

Next, as we have all the information we need, we can proceed with license removal.
Update variables $LicenseToRemove and $groupID with your IDs.

$LicenceToRemove    = "c42b9cae-ea4f-4ab7-9717-81576235ccac"
$groupID            = "GROUP OBJECT ID"

$apiUrl             = "https://graph.microsoft.com/v1.0/Groups/$groupID/assignLicense"

##

# REMOVE License Body for RestAPI
$body = @{
    addLicenses = @()
    removeLicenses= @($LicenceToRemove)
}

# Convert it to JSON
$jsonBody = $body | ConvertTo-Json

$removeLicense = Invoke-RestMethod -Method Post -Headers @{
    Authorization = "Bearer $($token)"
    'Content-Type'  = "application/json"
} -Body $jsonBody -Uri $apiUrl

###

$removeLicense

1 comment

  • feelie75

    FWIW, this is the c# code I created based on this article. Using the Rest.Sharp nuget library.

    request.AddJsonBody(new
          {
            addLicenses = new string[] { },
            removeLicenses = new[] { guid }
          });

Leave a Reply to feelie75 (Cancel Reply)

Related posts