Published: 30.Jun 2021

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

5 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
PowerAddict.net - Lubomir Goban  ---  Find me on - 
LinkedIn
0
Would love your thoughts, please comment.x
()
x