如何使用AzureAD v2模块移除用户的所有许可证?
使用AzureAD v2 PowerShell模块移除Microsoft 365用户的所有许可证
操作步骤:
1. 连接到Azure AD
先运行命令完成身份验证,确保使用的账号拥有用户管理员或全局管理员权限:Connect-AzureAD按提示完成登录即可。
2. 获取目标用户及当前许可证信息
替换<用户主体名>为目标用户的UPN(例如user@contoso.com),执行命令获取用户对象和已分配的许可证:$user = Get-AzureADUser -Filter "UserPrincipalName eq '<用户主体名>'" $currentLicenses = Get-AzureADUserLicenseDetail -ObjectId $user.ObjectId3. 构造许可证移除列表
提取当前所有许可证的SKU ID,整理成可用于移除操作的参数:$removeLicenses = $currentLicenses | Select-Object -ExpandProperty SkuId4. 执行许可证移除
调用Set-AzureADUserLicense完成移除操作:Set-AzureADUserLicense -ObjectId $user.ObjectId -RemoveLicenses $removeLicenses
补充说明:
- 如果目标用户原本无任何许可证,
$removeLicenses会是空数组,此时执行命令不会产生任何操作,也不会报错。 - 可添加判断逻辑提前确认用户许可证状态:
if ($currentLicenses.Count -gt 0) { Set-AzureADUserLicense -ObjectId $user.ObjectId -RemoveLicenses $removeLicenses Write-Host "已成功移除用户 $($user.UserPrincipalName) 的所有许可证" } else { Write-Host "用户 $($user.UserPrincipalName) 未分配任何许可证" }
内容的提问来源于stack exchange,提问作者Nova
相关产品推荐
相关产品推荐

