Reputation: 7451
Powershell -Command "cat .\tmp.txt | %{$_ -replace '\D', ''}"
Why running above Powershell command from .bat script doesn't work? It works only when I type it directly in Command Line...
Running from .bat script produces the following message:
Expressions are only allowed as the first element of a pipeline.
At line:1 char:39
+ cat .\tmp.txt | {$_ -replace '\D', ''} <<<<
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
Upvotes: 1
Views: 1200
Reputation: 3528
powershell /?
gives this help text (trimmed below to show only the relevant text).
-Command
...
To write a string that runs a Windows PowerShell command, use the format:
"& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.
So your batch file powrshell line needs to read:
powershell -Command "&{ cat .\tmp.txt | ForEach-Object {$_ -replace '\D', ''} }"
(note the extra curly braces wrapped around your command and %
being replaced with ForEach-Object
)
Upvotes: 4