Jacobs PostInformation TechnologyHow to Remove Exchange Online License via MS Graph
Jacobs PostInformation TechnologyHow to Remove Exchange Online License via MS Graph
Information TechnologyPowerShell

How to Remove Exchange Online License via MS Graph

This is a tutorial on how to remove Exchange Online Licenses from user accounts by leveraging Microsoft’s new Graph calls.

Pre-Requisites:

  • PowerShell Version 7 or later
  • .NET Framework 4.7.2 or later
  • Microsoft Graph Module Installed
  • Microsoft Entra Application Administrator, Cloud Application Administrator, or Global Administrator role(s)

Below are the commands to install and connect to Microsoft Graph:

Install Microsoft Graph PowerShell SDK:

Install-Module Microsoft.Graph -Scope CurrentUser

Verify Installation:

Get-InstalledModule Microsoft.Graph

Updating the SDK:

Update-Module Microsoft.Graph

Connect to MgGraph:

Connect-MgGraph -Scopes "User.Read.All", "Group.ReadWrite.All" -NoWelcome

PowerShell’s web sign-in prompt will appear where you can enter your Admin account credentials to authenticate and connect. The scopes require an account with the necessary privileges. The -NoWelcome parameter will omit the welcome text received in the terminal upon successful connection. Now that you are connected to Microsoft Graph, we can begin making calls to Graph.

#Commands to remove/clear licenses from user
Write-Host -Foreground "yellow" "Removing O365 Email License..."
$currentLicense = Get-MgUserLicenseDetail -UserId $deactivatedEmail | Select SkuId
Set-MgUserLicense -UserId $deactivatedEmail -RemoveLicenses $currentLicense.SkuId -AddLicenses @{}

Above is a snippet of some of the commands used in a script for deactivating email accounts in our Exchange Online environment. The “currentLicense” variable uses an MgGraph CMDlet under Microsoft.Graph.Users.Actions to gather license information. The -UserId parameter specifies the email account that needs to be modified. Finally, we pipeline that information and select the SkuId, which is just a fancy string of text for the license(s) the user is currently holding.

On the next line, we use the Set-MgUserLicense command to set the Exchange Online license details. Again, the same email account will be placed where the -UserId parameter is located. The -RemoveLicenses parameter is where we specify the previous variable we set which selected the SkuId. This will remove the license from the email account. Finally, the -AddLicenses parameter is specified which indicates that no licenses will be added in this case. If no errors occur, the licenses should now be removed. We can verify by using the command below:

Get-MgUserLicenseDetail -UserId

 If There are no entries for Id, SkuId, or SkuPartNumber then the user has no licenses assigned to the account. Congratulations, you have used PowerShell to remove a license from a user account! If you are confused on SKU, you can find some more information on the services that your company has subscribed to by executing the command below in PowerShell:

Get-MgSubscribedSKU | Format-List

This will show you the different types of licenses, including the Service Plans that each license holds. The Service Plan is useful for applying the license to a user account while assigning certain apps and features only. The apps included could be; SharePoint, Viva Engage Core, Microsoft Teams, Power Apps for Office 365, and more.

Disconnect from MgGraph:

Disconnect-MgGraph

Always disconnect from your sessions as good practice!

Hi, I’m Jacob

Hello! My name is Jacob and I am both the website owner and writer here at jacobspost.com. I love technology and sharing my experience and knowledge with others. Read more about in me in the 'About Me' page.

Leave a Reply

Your email address will not be published. Required fields are marked *