for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
- Below is my adaptation avoiding four unnecessary lines with
"skip=4", which can also be written without double quotes but needs to be properly escaped on the equals sign, where we use ^:
for /f skip^=4 %b in ('"echo;prompt;$E|cmd"') do set "_ESC=%b"
Output to stdout
>for /f %a in ('"echo;prompt;$E|cmd"') do @echo\output: %a
output: Microsoft
output: (c)
output: C:\Windows\System32>prompt;$E
output:
>
>for /f skip^=4 %a in ('"echo;prompt;$E|cmd"') do @echo\output: %a
output:
>
Output to file:
for /f skip^=4 %a in ('"echo;prompt;$E|cmd"') do @echo;%a>>File_With_Skip4.txt
for /f %a in ('"echo;prompt;$E|cmd"') do @echo;%a>>File_Without_Skip4.txt
Obs.1: Below command line converts the file to .\Hex_File_Without_Skip4.txt:
CertUtil -EncodeHex File_Without_Skip4.txt Hex_File_Without_Skip4.txt
Obs.2: The images that display "non-printable" characters here were obtained using Notepad++ and enabling the display of all characters:

Obs.3: See Commands Redirection for the behavior of > amd >> what is relevant to the point:
I can see that there are 4 CR/LF in the file.
If you are seeing 4 CR/LF even using skip^=4, please check your command and make sure you are overwriting or adding content to the output file.
Why is "skip=4" or skip=^4 needed - and not just skip=4?
When I redirected I only redirected the in( ) part of the, for /f command: Viz. C:\>echo;prompt;$E|cmd > result.txt
The CMD command calls the shell and by default returns the first 4 lines, after which it handles the redirected argument (echo;prompt;|), which produces/returns the Esc in the last (fifth) line:

Understand that the commands are treated by the command interpreter, and this uses predefined syntax rules, and in many occasions the use of one or more characters can be understood as a command complement, as an operator, as a redirector , etc. .. These characters assume a different behavior than the literal use, the documentation calls them special characters, and they can and will interfere differently if we use them without the escapes.
For more information, please read the linked references above.
Additional resources: