One of the most useful ways to log and troubleshoot the behavior of commands or batch jobs that you run on Windows is to redirect output to a file.
However, there are a few different ways you can redirect command line writes to a file. The option you choose depends on how you want to view your command output.
How Windows Command Prompt Output Works
When you type a command in the Windows console ( command prompt ), the output from that command goes to two separate streams.
- STDOUT: Standard Out is where any standard responses from commands go. For example the standard response for the DIR command is a list of files inside a directory.
- STDERR: Standard Error is where any error messages go if there’s a problem with the command. For example if there aren’t any files in the directory, the DIR command will output “File Not Found” to the Standard Error stream.
You can redirect output to a file in Windows for both of these output streams.
Redirect Standard Output Write to New File
There are two ways you can redirect standard output of a command to a file. The first is to send the command output write to a new file every time you run the command.
To do this, open the command prompt and type:
The > character tells the console to output STDOUT to the file with the name you’ve provided.
When you run this command , you’ll notice that there isn’t any response in the command window except the error that the file doesn’t exist.
This is because the standard output for the command was redirected to a file called myoutput.txt. The file now exists in the same directory where you ran the command. The standard error output still displays as it normally does.
Note: Be careful to change the active directory for the command prompt before running the command. This way you’ll know where the output files are stored.
You can view the standard output that went to the file by typing “myoutput.txt” in the command window. This will open the text file in your default text file viewer. For most people, this is usually Notepad.exe .
The next time you run the same command, the previous output file will be deleted. A new output file will be recreated with the latest command’s output.
Redirect Standard Output Writes to the Same File
What if you don’t want to overwrite the same file? Another option is to use >> rather than > to redirect to an output file. In the case of this example, you would type:
You’ll see the same output (the error only).
But in this case, instead of overwriting the output file, this command appends the new output to the existing output file.
Every time you run a command and append the output to a file, it’ll write the new standard output to the end of the existing file.
Redirect Standard Error To a File
The same way you can redirect standard output writes to a file, you can also output the standard error stream to a file.
To do this, you’ll need to add 2> to the end of the command, followed by the output error file you want to create.
In this example, you’ll type the command:
This sends the standard output stream to myoutput.txt, and the standard error stream to output.err. The result is that no output stream at all gets displayed in the console window.
However, you can see the error messages by typing output.err. This will open the file in your default text file viewer.
As you can see, any error messages from the command are output to the error file. Just as with the standard output, you can use >> instead to append the error to errors from previously run commands.
Redirect All Output Writes to a Same File
All of the approaches above result in multiple files. One file is for the standard output stream and the other is for the standard error stream.
If you want to include both of these outputs to the same file, you can do that too. To do this, you just need to redirect all output to the same file using the following command.
Here’s how this command works:
- The standard output is directed to the output file identified by output number 1.
- The standard error output identified by the number 2 is redirected to the output file identified by number 1.
This will append the error output to the end of the standard output.
This is a useful way to see all output for any command in one file.
Silencing Standard or Error Output Streams
You can also turn off either Standard Output or Standard Error by redirecting the output to a NUL instead of a file.
Using the example above, if you only want Standard Output and no Standard Error at all, you can use the following command:
This will result in the same output file as the first example above where you only redirected the Standard Output, but with this command the error won’t echo inside the console. It won’t create an error log file either.
This is useful if you don’t care about any errors and don’t want them to become a nuisance.
You can perform any of the same output commands above from inside a BAT file and the output from that line will go to the output file you specify. This is a useful way to see whether any commands within a BAT file had any errors when they tried to run.