Search code examples
batch-filetasklist

How to echo a string with a pipe character in a batch script?


How can I make a text file with the given line using batch script:

tasklist /FI "IMAGENAME eq chrome.exe" | find /i "chrome.exe" 

I created a batch file and put this code below:

echo tasklist /FI "IMAGENAME eq chrome.exe" | find /i "chrome.exe" >>d:\dblank.txt

It created a dblink.text file but when I opened the dblink.txt the only text was:

tasklist /FI "IMAGENAME eq chrome.exe" 

Why didn't it copy the code:

| find /i "chrome.exe"

How can I copy the whole text?


Solution

  • Escape the pipe character

    echo tasklist /FI "IMAGENAME eq chrome.exe" ^| find /i "chrome.exe" >>d:\dblank.txt
    

    Without this escape, is it considered a real pipe operation request, and find is called to filter the output of the echo command.