1

I try to invoke a PowerShell command within a batch file:

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

But it always fails with this error:

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

I try with single quotes for the replace strings:

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

But the backtick escape character is treated like any other text character when it appears within a string enclosed in single quotes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
I M
  • 13
  • 4

1 Answers1

1

Just escape the double quotes using a backslash:

powershell -Command "(gc test.txt ) -replace (\"~\[\",\"`r`n[\") | sc test.txt"
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172