5000+ Great Articles

Redirect Output from the Windows Command Line to a Text File

One of the most useful ways to register and troubleshoot commands or batch jobs running on Windows is by redirecting the output to a file.

However, there are several different ways to redirect command line writing to a file. The option you choose depends on how you want to view the command output.

How does Windows Command Prompt output work

When you enter a command in the Windows console (command line), the output from that command comes in two separate streams.

You can redirect output to a file on Windows for both of these output streams.

Redirect writing standard output to a new file

There are two ways to redirect the standard output of a command to a file. The first is to send the output of the write command to a new file every time you run the command.

To do this, open a command prompt and enter:

– / dir test.exe> ??myoutput.txt

The> symbol tells the console to output STDOUT to a file with the name you specify.

When you run this command, you will notice that there is no response in the command window other than the error that the file does not exist.

This is because the standard output of the command was redirected to a file named myoutput.txt. The file now exists in the same directory where you ran the command. Standard error output is still displayed as usual.

Note. Be careful to change the active directory for the command line before running the command. This way you will know where the output files are stored.

You can view the standard output sent to the file by typing “myoutput.txt” in the command window. This will open a text file in the 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 with the results of the last command will be recreated.

Redirecting standard output writes to the same file

What if you don’t want to overwrite the same file? Another option is to use >> instead of> to redirect to the output file. In the case of this example, you would enter:

dir test.exe >> myoutput.txt

You will see the same result (error only).

But in this case, instead of overwriting the output file, this command adds new output to the existing output file.

Each time you run a command and add output to a file, new standard output will be written to the end of the existing file.

Standard error redirect to file

Just as you can redirect the standard output of a write to a file, you can also output standard error to a file.

To do this, you need to add 2> to the end of the command and then the error output file you want to create.

In this example, you type the command:

dir test.exe> ??myoutput.txt 2> output.err

This sends standard output to myoutput.txt and standard error to output.err. As a result, no output stream is 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. As with standard output, you can use >> instead to add an error to errors from previously executed commands.

Redirect all output write operations to the same file

All of the above approaches result in multiple files being created. One file is for standard output and the other is for standard error.

If you want to include both of these outputs in one file, you can do that too. To do this, you just need to redirect all output to the same file using the following command.

dir test.exe 1> myoutput.txt 2> & 1

This is how it works:

This will add the error output to the end of the standard output.

This is a useful way to see all the output of any command in a single file.

Standard mute or error output streams

You can also turn off standard output or standard error by redirecting the output to NUL instead of a file.

Using the above example, if you only want standard output and don’t need standard error, you can use the following command:

dir test.exe 1> myoutput.txt 2> nul

This will lead to the same output file as in the first example above, where you only redirected standard output, but with this command, the error will not be displayed inside the console. It also won’t create an error log file.

This is useful if you don’t care about bugs and don’t want them to get in the way.

You can execute any of the above output commands from a BAT file, and the output from that line will go to the output file you specified. This is a useful way to find out if any commands in the BAT file were errors when trying to run them.

Exit mobile version