使用 PowerShell 從使用者帳戶移除 Microsoft 365 授權
本文適用於 Microsoft 365 企業版和 Office 365 企業版。
注意事項
瞭解如何使用 Microsoft 365 系統管理中心 從用戶帳戶移除授權。 如需其他資源的清單,請 參閱管理使用者和群組。
使用 Microsoft Graph PowerShell SDK
指派和移除使用者的授權需要 User.ReadWrite.All 許可權範圍或 [指派授權] 參考頁面中所列的其中一個其他許可權 圖形 API。
需要 Organization.Read.All 許可權範圍,才能讀取租用戶中可用的授權。
Connect-Graph -Scopes User.ReadWrite.All, Organization.Read.All
若要檢視貴組織的授權方案資訊,請參閱下列文章:
從用戶帳戶移除授權
若要從現有的使用者帳戶移除授權,請使用下列語法:
Set-MgUserLicense -UserId "<Account>" -RemoveLicenses @("<AccountSkuId1>") -AddLicenses @{}
此範例會從使用者BelindaN@litwareinc.com移除SPE_E5 (Microsoft 365 E5) 授權方案:
$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
Set-MgUserLicense -UserId "belindan@litwareinc.com" -RemoveLicenses @($e5Sku.SkuId) -AddLicenses @{}
若要從現有授權使用者群組中移除所有授權,請使用下列語法:
$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 @{}
}
若要從檔案中的 CSV
使用者清單中移除特定授權,請執行下列步驟。 本範例會從 C:\My Documents\Accounts.csv 檔案中CSV
定義的使用者帳戶中移除 SPE_E5 (Microsoft 365 企業版 E5) 授權。
建立 CSV 檔案並將其儲存至 C:\My Documents\Accounts.csv,其標頭下
UserPrincipalName
的每一行各包含一個帳戶,如下所示:UserPrincipalName akol@contoso.com tjohnston@contoso.com kakers@contoso.com
使用下列命令:
$usersList = Import-CSV -Path "C:\My Documents\Accounts.csv" $e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5' foreach($user in $usersList) { Set-MgUserLicense -UserId $user.UserPrincipalName -RemoveLicenses @($e5Sku.SkuId) -AddLicenses @{} }
另一種釋出授權的方式是刪除用戶帳戶。 如需詳細資訊,請參閱 使用PowerShell刪除和還原用戶帳戶。
另請參閱
以 PowerShell 管理 Microsoft 365 使用者帳戶、授權和群組