Batch Script - Parenthesized echo of the word "Where" issue
You have misdiagnosed the problem - WHERE has nothing to do with it.
You simply need to escape the closing parenthesis:
@ECHO ON
(
ECHO [System.IO.DriveInfo]::GetDrives(^) ^| Where {$_.Name -like "C:\"}
)>> Outfile.txt
PAUSE
Here is one other option for you. This doesn't require you to escape the parentheses or the pipe. When you redirect the NUL device to the SET /P
command it writes the prompt string to stdout. The one caveat to this is that SET /P
doesn't write out a carriage return and line feed so you need an empty ECHO.
to output the CRLF.
@ECHO OFF
(
<nul set /p "=[System.IO.DriveInfo]::GetDrives() | Where {$_.Name -like "C:\"}" &echo.
echo another line
)>Outfile.txt
notepad Outfile.txt
PAUSE
For what it’s worth,
it seems that, after a )
, CMD interprets ^|
as |
:
C:\> (echo 1.The && echo 2.quick && echo 3.brown && echo 4.fox) ^| find "q"
2.quick