自动化卸载指定.NET及Desktop Runtime包遇阻求助
离线环境下彻底卸载.NET 6.0.18相关组件方案
目标卸载组件
-.NET Host FX Resolver (x64 + x86)
-.NET Host Host (x64 + x86)
-.NET Runtime (x64 + x86)
-ASP.NET Core 6.0.18 共享框架 (x64 + x86)
-ASP.NET Core 6.0.18 托管捆绑选项
-Windows Desktop Runtime (x64 + x86)
已尝试无效的操作
- 通过CSV批量调用
Msiexec.exe /X $dir $arguments,无报错但组件残留于「添加或删除程序」 - 使用
Get-Package -Name “6.0.18” -AllVersions | Uninstall-Package,执行成功但残留未清除 - 尝试dotnet-core-uninstall-1.6.0工具,无法识别全部目标包,且不愿额外安装工具
方法1:通过产品代码精准卸载
.NET组件的卸载需依赖MSI产品代码而非文件路径,以下两种方式可获取并执行卸载:
方式A:通过WMI获取产品代码(适用于所有MSI安装的组件)
# 筛选出所有含6.0.18的.NET相关组件 $targetPackages = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -match "6.0.18" -and $_.Name -match "\.NET|ASP\.NET|Windows Desktop Runtime" } # 静默卸载每个组件,/qn表示无交互,/norestart避免强制重启 foreach ($pkg in $targetPackages) { Write-Host "正在卸载: $($pkg.Name)" Start-Process msiexec.exe -ArgumentList "/X $($pkg.IdentifyingNumber) /qn /norestart" -Wait -NoNewWindow }
方式B:通过注册表获取产品代码(更高效,无MSI验证开销)
# 遍历x64和x86的卸载注册表路径 $regPaths = @( "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" ) # 提取目标组件的产品代码 $productCodes = Get-ItemProperty -Path $regPaths | Where-Object { $_.DisplayName -match "6.0.18" -and $_.DisplayName -match "\.NET|ASP\.NET|Windows Desktop Runtime" -and $_.UninstallString -match "msiexec" } | Select-Object DisplayName, @{Name="ProductCode"; Expression={$_.UninstallString -replace "msiexec.exe /X", "" -replace " /.*", ""}} # 执行卸载 foreach ($item in $productCodes) { Write-Host "正在卸载: $($item.DisplayName)" Start-Process msiexec.exe -ArgumentList "/X $($item.ProductCode.Trim()) /qn /norestart" -Wait -NoNewWindow }
方法2:清理「添加或删除程序」残留项
卸载后若仍有残留,直接清理对应注册表项:
$regPaths = @( "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" ) Get-ItemProperty -Path $regPaths | Where-Object { $_.DisplayName -match "6.0.18" -and $_.DisplayName -match "\.NET|ASP\.NET|Windows Desktop Runtime" } | ForEach-Object { Write-Host "删除残留注册表项: $($_.PSPath)" Remove-Item -Path $_.PSPath -Recurse -Force -ErrorAction SilentlyContinue }
方法3:清理残留文件目录
卸载后手动删除.NET 6.0.18的共享框架目录:
$targetDirs = @( "${env:ProgramFiles}\dotnet\shared\Microsoft.NETCore.App\6.0.18", "${env:ProgramFiles(x86)}\dotnet\shared\Microsoft.NETCore.App\6.0.18", "${env:ProgramFiles}\dotnet\shared\Microsoft.AspNetCore.App\6.0.18", "${env:ProgramFiles(x86)}\dotnet\shared\Microsoft.AspNetCore.App\6.0.18", "${env:ProgramFiles}\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.18", "${env:ProgramFiles(x86)}\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.18" ) foreach ($dir in $targetDirs) { if (Test-Path $dir) { Write-Host "删除残留目录: $dir" Remove-Item -Path $dir -Recurse -Force -ErrorAction SilentlyContinue } }
内容的提问来源于stack exchange,提问作者usteve94
相关产品推荐
相关产品推荐

