Add/remove license 365 bằng CLI

Chạy PowerShell

Kết nối dịch vụ:

Connect-MgGraph -Scopes User.ReadWrite.All

Xong thì làm với user muốn gỡ lic: 

 # User
$userId = "linhlm@domain.com"

# Get current lic
$currentLicenses = (Get-MgUser -UserId $userId -Property AssignedLicenses).AssignedLicenses.SkuId

# List lic to remove, bao gom mailbox + teams
$targetLicenses = @(
    "4b585984-651b-448a-9e53-3b10f069cf7f",  # F3
    "f8ced641-8e17-4dc5-b014-f5a2d53f6ac8",  # E1
    "46c3a859-c90d-40b3-9551-6178a48d5c18",  # E3
    "3ab6abff-666f-4424-bfb7-f0bc274ec7bc",  # Teams Ess
    "7e31c0d9-9551-471d-836f-32ee72be4a01"   # Teams Ent
)

# Remove lic
$removeLicenses = $targetLicenses | Where-Object { $_ -in $currentLicenses }

if ($removeLicenses.Count -gt 0) {
    Set-MgUserLicense -UserId $userId -AddLicenses @() -RemoveLicenses $removeLicenses
    Write-Output "Removed licenses: $removeLicenses"
}
else {
    Write-Output "$userId EMPTY license"
}

No comments:

Post a Comment

MS 365 chạm giới hạn số lượng xóa user cùng lúc

Để tránh xóa nhầm, trên hệ thống AAD Connect sẽ có giới hạn số lượng user xóa trong cùng 1 lượt:  https://learn.microsoft.com/en-us/entra/id...