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

如何使用PowerShell仅删除指定路径下的zip压缩文件夹?

PowerShell删除指定路径下zip压缩包的修正方案

原脚本错误原因

  • 未给Get-ChildItem指定检索路径,默认只会检索当前PowerShell工作目录的文件,不会命中你定义的$TestPath下的zip文件
  • if判断语法不合法,$TestPath $_.BaseName不是可执行的判断逻辑
  • Remove-Item仅传入$_.BaseName,同样只会在当前工作目录查找目标,路径匹配失败自然无法完成删除

最终可执行脚本

场景1:直接删除路径下所有.zip格式压缩包

$TestPath='D:\RAW\St\Documentum\2021-10-12\VPA_IMAGES'
Get-ChildItem -Path $TestPath -Filter *.zip -File | Remove-Item -Force

场景2:确认存在和zip同名的普通文件夹后,再删除对应zip压缩包(匹配原脚本的设计逻辑)

$TestPath='D:\RAW\St\Documentum\2021-10-12\VPA_IMAGES'
Get-ChildItem -Path $TestPath -Filter *.zip -File | ForEach-Object {
    $targetFolder = Join-Path -Path $TestPath -ChildPath $_.BaseName
    if (Test-Path -Path $targetFolder -PathType Container) {
        Remove-Item -Path $_.FullName -Force
    }
}

补充说明

如果你的需求是删除和zip同名的普通文件夹,只需要把Remove-Item的参数换成$targetFolder即可:

Remove-Item -Path $targetFolder -Recurse -Force

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 23:27:04