You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MSOnline模块已安装但Get-MsolUserLicense未识别,求卸365许可替代方法

解决PowerShell移除Office365许可证的问题及脚本修正

错误原因

你碰到的Get-MsolUserLicense未被识别的问题,核心是这个cmdlet根本不存在于MSOnline模块中。MSOnline模块里获取用户许可证信息,需通过Get-MsolUser的Licenses属性实现。

替代方案1:修正MSOnline模块的用法

把脚本中获取并移除许可证的代码段替换为以下内容:

# 移除用户所有许可证
$userObj = Get-MsolUser -UserPrincipalName $User
if ($userObj.Licenses) {
    $licenseSkuIds = $userObj.Licenses | Select-Object -ExpandProperty AccountSkuId
    Set-MsolUserLicense -UserPrincipalName $User -RemoveLicenses $licenseSkuIds
} else {
    Write-Host "该用户没有已分配的许可证"
}

替代方案2:使用Microsoft Graph PowerShell模块(推荐)

MSOnline模块已被微软弃用,建议改用Microsoft Graph PowerShell模块,功能更稳定且覆盖场景更全:

  1. 安装模块(未安装时执行):
if (-not (Get-Module -ListAvailable -Name Microsoft.Graph.Users)) {
    Install-Module -Name Microsoft.Graph.Users -Force -AllowClobber
}
  1. 连接到Microsoft Graph:
Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All"
  1. 移除用户许可证的代码:
# 获取用户的许可证分配ID并移除
$userLicenses = Get-MgUserLicenseDetail -UserId $User
if ($userLicenses) {
    $removeParams = @{
        UserId = $User
        RemoveLicenses = $userLicenses.SkuId
    }
    Update-MgUserLicense @removeParams
} else {
    Write-Host "该用户没有已分配的许可证"
}

脚本其他问题修正

你的脚本里还有一处变量错误:$userEmail未定义,需替换为$User(因为你读取的是用户的UserPrincipalName),否则分发组移除成员的逻辑会失效。修正后代码:

Get-DistributionGroup | Where-Object { (Get-DistributionGroupMember $_).PrimarySmtpAddress -contains $User } | ForEach-Object { 
    Remove-DistributionGroupMember -Identity $_.Name -Member $User -Confirm:$false 
}

内容的提问来源于stack exchange,提问作者Shaggy89

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 15:07:45