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
相关产品推荐
相关产品推荐

