Save the Command’s Output to File

How to Save the Command Prompt’s Output to File in Windows?

There are basic and commonly used ways to redirected to file ( pipe )  standard error (stderr) and/or  standard output( stdout)

Overview:

Writing  the output of Windows Command is important to get a copy of the command output and the error in a file  for further analysis and  to share with the teams. often used when debugging  application or  write the output of scheduled  task to a  file to check and analysis the result.

 

  • command > output.txt

The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.

  • command >> output.txt

The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

 

Numeric handles:

Success and failure are based on the Exit Code of the command.

STDIN  = 0  Keyboard input

STDOUT = 1  Text output

STDERR = 2  Error text output

UNDEFINED = 3-9

 

  • command 2> output.txt

Redirect any error message into a file, the standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.

  • command 2>> output.txt

Append any error message into a file, the standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

  • command > output.txt 2>&1

Redirect errors and output to one file, both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, it gets overwritten.

  • command >> output.txt 2>&1

Redirect errors and output to one file, both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

  • command > output.txt 2> output2.txt

Redirect output and errors to separate files, nothing will be visible in the terminal. . If the file already exists, it gets overwritten.

 

Examples:

   DIR > output.txt

   DIR C:\ > output.txt 2>errorlog.txt

   DIR C:\ > output.txt & DIR D:\ > output2.txt

 

Good luck!

I hope this post will be useful to you. If you like the post , Please don’t forget to Vote and click the Like Button.

Email me at itmug.pro@gmail.com for corrections, additions, or questions.

Leave a comment