1

I am starting several powershell instances from a batch script.

With one, I'd like to tail a file and add a timestamp to each line. The command itself works well in powershell directly:

filter timestamp {"$(Get-Date -Format G): $_"}; Get-Content .\MyLog.txt -tail 10 -wait | Select-String 'Search term' | timestamp

But when I use the following command within cmd, I get errors:

start powershell -Command "filter timestamp {"$(Get-Date -Format G): $_"}; Get-Content .\MyLog.txt -tail 10 -wait | Select-String 'Search term' | timestamp"; pause

Example for an error:

In Zeile:1 Zeichen:18
+ start powershell -Command "filter timestamp {"$(Get-Date -Format G):  ...
+                  ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand 

I wonder if I need to escape? Have tried many options but after hours I need some pro help.

Thanks a lot.

Fabian
  • 35
  • 5
  • What "*errors*"? – Abraham Zinala Jan 09 '22 at 22:52
  • I just added an example. As mentioned, I believe it's becaue not not escaping right. I tried so many different versions but none of them worked. – Fabian Jan 09 '22 at 23:02
  • What version of PowerShell are you on? – Santiago Squarzon Jan 09 '22 at 23:09
  • 1
    you have to escape the double quotes. You're error is also from within PowerShell, not Cmd.exe as the screenshot shows. That specific error refers to a syntactically incorrect annotation. You're calling on `Start-Process` with an argument of `PowerShell.exe`, and then you kind of just left everything else there. PowerShell parses separate words as tokens for that cmdlet, or in this case, parameters. In other words, `Start-Process` does not have a parameter of `-Command` which is why you're seeing that error. – Abraham Zinala Jan 09 '22 at 23:52

1 Answers1

2

In Powershell, there are several ways to include a double-quote character in a string that is between double-quotes (i.e "). That's the problem with your command line, when started from a CMD prompt.

To solve your issue, use """ in the -Command string instead of " (except of course for the start and end of your command). See below:

start powershell -Command "filter timestamp {"""$(Get-Date -Format G): $_"""}; Get-Content .\MyLog.txt -tail 10 -wait | Select-String 'Search term' | timestamp"; pause

Also, instead of adding the pause at the end of your command, you can use the -NoExit switch from powershell.exe:

start powershell -NoExit -Command "filter timestamp {"""$(Get-Date -Format G): $_"""}; Get-Content .\MyLog.txt -tail 10 -wait | Select-String 'Search term' | timestamp"
mklement0
  • 382,024
  • 64
  • 607
  • 775
ZivkoK
  • 366
  • 3
  • 6
  • Nicely done. Note that while `"""` works (with `powershell.exe`, but not `pwsh.exe`, the PowerShell (Core) v6+ CLI), it isn't _fully robust_ when calling from `cmd.exe`. You could more simply use `\"` (in _both_ PowerShell editions), but this too isn't fully robust. A fully robust solution requires `"^""` (sic) for `powershell.exe` and `""` for `pwsh.exe` - see [this answer](https://stackoverflow.com/a/49060341/45375). – mklement0 Jan 11 '22 at 04:18