Easiest way to add a text to the beginning of another text file in Command Line (Windows) Easiest way to add a text to the beginning of another text file in Command Line (Windows) windows windows

Easiest way to add a text to the beginning of another text file in Command Line (Windows)


echo "my line" > newFile.txttype myOriginalFile.txt >> newFile.txttype newFile.txt > myOriginalFile.txt

Untested.Double >> means 'append'


Another variation on the theme.

(echo New Line 1) >file.txt.newtype file.txt >>file.txt.newmove /y file.txt.new file.txt

Advantages over other posted answers:

  • minimal number of steps
  • no temp file left over
  • parentheses prevents unwanted trailing space in first line
  • the move command "instantaneously" replaces the old version with the new
  • the original file remains unchanged until the last instant when it is replaced
  • the final content is only written once - potentially important if the file is huge.


The following sequence will do what you want, adding the line "new first line" to the file.txt file.

ren file.txt temp.txtecho.new first line>file.txttype temp.txt >>file.txtdel temp.txt

Note the structure of the echo. "echo." allows you to put spaces at the beginning of the line if necessary and abutting the ">" redirection character ensures there's no trailing spaces (unless you want them, of course).