无法移除PowerShell模块Pester 3.4.0,寻求解决办法
无法删除Pester v3.4.0模块的解决办法?
问题详情
尝试移除Pester v3.4.0模块,但所有操作均失败:
- 以管理员身份手动删除文件夹无效
- 正常或管理员权限下运行
Uninstall-Module无效 - 使用指定脚本也无法完成
使用的脚本内容:
# folders where PowerShell looks for modules: $paths = $env:PSModulePath -split ';' # finding actual module folders $modules = Get-ChildItem -Path $paths -Depth 0 -Directory | Sort-Object -Property Name $modules | Select-Object -Property Name, @{N='Parent';E={$_.Parent.FullName}}, FullName | Out-GridView -Title 'Select module(s) to permanently delete' -PassThru | Out-GridView -Title 'Do you REALLY want to remove the modules below? CTRL+A and OK to confirm' -PassThru | Remove-Item -Path { $_.FullName } -Recurse -Force
执行脚本后出现以下错误:
+ Remove-Item -Path { $_.FullName } -Recurse -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (3.4.0:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand Remove-Item : Cannot remove item C:\Program Files\WindowsPowerShell\Modules\Pester: The directory is not empty. At line:5 char:3
已尝试的操作:
- 在提升权限的PowerShell、普通PowerShell、提升权限的pwsh7.4.2、普通pwsh7.4.2,以及普通和提升权限的VSCode中操作
- 直接删除文件夹
- 执行
Get-Process -Name powershell,pwsh | kill -force终止相关进程,关闭所有VSCode进程,重启电脑
get-module -listavailable命令输出:
get-module -listavailable | where name -EQ 'pester' Directory: C:\Program Files\WindowsPowerShell\Modules ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Script 3.4.0 Pester {Describe, Context, It, Should...}
可行解决步骤
1. 查找并终止占用进程
用PowerShell命令查找是否有进程正在占用Pester模块的文件:
Get-Process | Where-Object { $_.Modules.FileName -like "*Pester*" } | Select-Object Id, Name, Path
如果返回结果,执行以下命令强制终止:
Get-Process | Where-Object { $_.Modules.FileName -like "*Pester*" } | Stop-Process -Force
之后再尝试删除文件夹。
2. 使用Robocopy清空文件夹
创建一个空文件夹(比如C:\EmptyTemp),然后用Robocopy镜像替换清空Pester文件夹:
robocopy C:\EmptyTemp "C:\Program Files\WindowsPowerShell\Modules\Pester" /MIR
执行完成后,再删除Pester文件夹。
3. 进入安全模式删除
重启电脑进入安全模式,然后手动导航到C:\Program Files\WindowsPowerShell\Modules,删除Pester文件夹。安全模式下几乎没有第三方进程运行,能避免文件被占用。
4. 检查并修复文件夹权限
右键Pester文件夹 → 属性 → 安全 → 高级:
- 点击「更改权限」,确保当前管理员账户拥有「完全控制」权限
- 勾选「替换所有子对象的权限条目」,点击应用
- 完成后再尝试删除文件夹
内容的提问来源于stack exchange,提问作者Matt Williamson
相关产品推荐
相关产品推荐

