批处理文件中的PowerShell命令,其中命令中需要双引号

时间:2016-06-13 11:56:42

标签: powershell batch-file

我尝试在批处理文件中调用PowerShell命令:

powershell -Command "(gc test.txt ) -replace ("~\[","`r`n[") | sc test.txt"

但它始终因此错误而失败:

At line:1 char:29
+ (gc test.txt ) -replace (~\[,`r`n[) | sc test.txt
+                             ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
   ception
    + FullyQualifiedErrorId : MissingArgument

我尝试使用单引号代替替换字符串:

powershell -Command "(gc test.txt ) -replace ('~\[','`r`n[') | sc test.txt"

但是当反引号转义符出现在用单引号括起的字符串中时,它被视为任何其他文本字符。

1 个答案:

答案 0 :(得分:1)

使用反斜杠转义双引号:

powershell -Command "(gc test.txt ) -replace (\"~\[\",\"`r`n[\") | sc test.txt"