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

PowerShell批处理命令中使用-notlike时无法转义反斜杠的问题求助

PowerShell批处理命令中使用-notlike时无法转义反斜杠的问题求助

大家好,我遇到了一个PowerShell结合批处理使用的小问题,想请各位帮忙分析下原因。

我原本有个能在批处理文件里正常运行的PowerShell命令,用来移除%tempFile%中所有包含字符串foo的行,命令如下:

powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '*foo*' }) | set-content $_.FullName -Force }"

而且我发现-notlike就算不带通配符星号也能生效,比如写成这样也没问题:

| ? { $_ -notlike 'foo' }) |

现在我需要移除%tempFile%里包含反斜杠\的行。按照常规的转义思路,我尝试用双反斜杠\\来转义,这个方法在replace命令里是有效的,但放到上面的-notlike命令里却不管用。我试了以下几种写法,结果都失败了:

  • 写法1:
powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '*\\*' }) | set-content $_.FullName -Force }"
  • 写法2:
powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '*\*' }) | set-content $_.FullName -Force }"
  • 写法3:
powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '\\' }) | set-content $_.FullName -Force }"
  • 写法4:
powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '\' }) | set-content $_.FullName -Force }"

我实在搞不懂为什么这些写法都不生效,有没有朋友能帮我理清这个问题?提前谢谢大家了!

备注:内容来源于stack exchange,提问作者bat_cmd

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 13:58:11