Your problem has nothing to do with the pipe. Your problem is how to escape the command string properly such that the assignment includes an odd number of quotes in the value, and then your ECHO command properly displays the resultant value. You have the exact same problem if you remove the pipe and simply execute the CMD.EXE command on its own.
Before I provide a solution, you definitely should have a look at Why does delayed expansion fail when inside a piped block of code? for an in depth exploration of the intricacies of complex pipe operations.
There are a few things that complicate your problem:
1) The command string gets parsed multiple times - first by the main batch script, in which case you must make sure that the &
is either quoted or escaped such that it is passed through to CMD.EXE. The second time it is parsed by CMD.EXE itself, and this time you need to somehow include a quote in the assignment, yet now the &
must not be escaped or quoted such that it properly fires the ECHO command to show the result.
2) The outer most quotes enclosing the command string are removed by CMD.EXE prior to execution. But it is helpful to know that the outer quotes are not required :-) You simply must find the correct escape and quote sequences.
There are many solutions, and it is entirely predictable. However it can easily make your head explode when trying to trace through how it works ;-)
All of the following work. I added square brackets around the output to demonstrate that no unwanted characters are included in the assignment. The unwanted IGNORED text is just to demonstrate how a quoted assignment ignores text that follows the last quote. The IGNORED text can simply be removed.
@echo off
:: Without quotes around the command
echo | cmd.exe /v:on /c set z=Car^^" and Airplane is Machine&echo [!z!]
echo | cmd.exe /v:on /c set z=Car^^^" and Airplane is Machine^&echo [!z!]
:: With quotes around the command
echo | cmd.exe /v:on /c "set z=Car^" and Airplane is Machine^&echo [!z!]"
echo | cmd.exe /v:on /c ^"set z=Car^^" and Airplane is Machine&echo [!z!]"
:: With quotes around the assignment such that text after the last quote is ignored
:: Without quotes around the command
echo | cmd.exe /v:on /c set "z=Car" and Airplane is Machine^^" IGNORED & echo [!z!]
echo | cmd.exe /v:on /c set ^^"z=Car" and Airplane is Machine" IGNORED & echo [!z!]
echo | cmd.exe /v:on /c set "z=Car" and Airplane is Machine^^^" IGNORED ^& echo [!z!]
echo | cmd.exe /v:on /c set ^^"z=Car" and Airplane is Machine^" IGNORED ^& echo [!z!]
:: With quotes around the assignment such that text after the last quote is ignored
:: With quotes around the command
echo | cmd.exe /v:on /c "set "z=Car" and Airplane is Machine^" IGNORED ^& echo [!z!]"
echo | cmd.exe /v:on /c "set ^"z=Car" and Airplane is Machine" IGNORED ^& echo [!z!]"
echo | cmd.exe /v:on /c "set "z=Car^" and Airplane is Machine^^" IGNORED & echo [!z!]"
echo | cmd.exe /v:on /c "set ^"z=Car^" and Airplane is Machine" IGNORED & echo [!z!]"
--RESULT--
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
Surely your actual code is significantly different. I wouldn't be surprised if you have some additional work to do when you try to apply these concepts to your actual problem. Good luck!