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

批处理文件中的PowerShell命令,其中命令中需要双引号,powershell,batch-file,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

我尝试在批处理文件中调用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"
但是,当反斜杠转义字符出现在单引号内的字符串中时,它将被视为与任何其他文本字符一样。

只需使用反斜杠转义双引号即可:

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

单引号甚至不是必需的,转义双引号就足够了。@GeraldSchneider如果我不将命令放在单引号中,我会出错-不是吗?当我将命令放在单引号中时,我只会得到错误
error:unrecogned command
。@GeraldSchneider你是对的,我在PowerShell中测试过这个。。。谢谢你的提示。powershell-命令“(gc test.txt)-replace(\“~[\”,\“
r
n[\”)\124; sc test.txt”有效。谢谢。