Επεξεργασία

Κοινή χρήση μέσω


Remove Microsoft 365 licenses from user accounts with PowerShell

This article applies to both Microsoft 365 Enterprise and Office 365 Enterprise.

Note

Learn how to remove licenses from user accounts with the Microsoft 365 admin center. For a list of additional resources, see Manage users and groups.

Use the Microsoft Graph PowerShell SDK

First, connect to your Microsoft 365 tenant.

Assigning and removing licenses for a user requires the User.ReadWrite.All permission scope or one of the other permissions listed in the 'Assign license' Graph API reference page.

The Organization.Read.All permission scope is required to read the licenses available in the tenant.

Connect-Graph -Scopes User.ReadWrite.All, Organization.Read.All

To view the licensing plan information in your organization, see the following articles:

Removing licenses from user accounts

To remove licenses from an existing user account, use the following syntax:

Set-MgUserLicense -UserId "<Account>" -RemoveLicenses @("<AccountSkuId1>") -AddLicenses @{}

This example removes the SPE_E5 (Microsoft 365 E5) licensing plan from the user BelindaN@litwareinc.com:

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
Set-MgUserLicense -UserId "belindan@litwareinc.com" -RemoveLicenses @($e5Sku.SkuId) -AddLicenses @{}

To remove all licenses from a group of existing licensed users, use the following syntax:

$licensedUsers = Get-MgUser -Filter 'assignedLicenses/$count ne 0' `
    -ConsistencyLevel eventual -CountVariable licensedUserCount -All `
    -Select UserPrincipalName,DisplayName,AssignedLicenses

foreach($user in $licensedUsers)
{
    $licensesToRemove = $user.AssignedLicenses | Select -ExpandProperty SkuId
    $user = Set-MgUserLicense -UserId $user.UserPrincipalName -RemoveLicenses $licensesToRemove -AddLicenses @{} 
}

To remove a specific license from a list of users in a text file, perform the following steps. This example removes the SPE_E5 (Microsoft 365 Enterprise E5) license from the user accounts defined in the text file C:\My Documents\Accounts.txt.

  1. Create and save a text file to C:\My Documents\Accounts.txt that contains one account on each line like this:

    akol@contoso.com
    tjohnston@contoso.com
    kakers@contoso.com
    
  2. Use the following command:

    $x=Get-Content "C:\My Documents\Accounts.txt"
    $e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
    for ($i=0; $i -lt $x.Count; $i++)
    {
    Set-MgUserLicense -UserId $x[$i] -RemoveLicenses @($e5Sku.SkuId) -AddLicenses @{}
    }
    

Another way to free up a license is by deleting the user account. For more information, see Delete and restore user accounts with PowerShell.

See also

Manage Microsoft 365 user accounts, licenses, and groups with PowerShell

Manage Microsoft 365 with PowerShell

Getting started with PowerShell for Microsoft 365