How can I redirect Windows cmd standard output and standard error to a single file?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Windows cmd, normal output and error output are separate streams, and combining them into one log file is a very common scripting task. The trick is simple once you know it, but operator order matters because cmd evaluates redirection from left to right.
Know the Two Streams You Are Redirecting
In cmd, the main streams are:
- stream
1for standard output - stream
2for standard error
Common operators are:
>to overwrite a file with standard output>>to append output to a file2>to redirect only standard error2>&1to send standard error to wherever standard output currently goes
Once you understand that last rule, the combined redirection syntax becomes much easier to remember.
Use > file 2>&1 for One Combined Log
The standard pattern is:
This works because:
> output.logsends stream1to the file2>&1then sends stream2to the same destination as stream1
If you want append mode instead of overwrite mode, use:
That is the most common pattern for batch-job logs and scheduled task output.
Why Order Matters
These two commands are not equivalent:
In the second form, stream 2 is redirected to the current destination of stream 1, which is still the console at that moment. Then only stream 1 is redirected to the file. The result is split output instead of a unified log.
That is why the file redirection must appear before 2>&1.
Redirect Multiple Commands at Once
If you are running several commands in a batch file, group them and apply one redirection to the whole block.
This keeps the log ordered and avoids repeating the same redirection syntax on every line.
Redirect Only Errors or Silence Everything
Sometimes you want a more selective setup. For example, to capture only errors:
To discard both normal output and errors:
Silencing both streams can be useful for noisy checks, but it also removes evidence you might need during troubleshooting, so use it sparingly.
Use It in Batch Scripts With Exit Handling
Redirection becomes more useful when paired with explicit error handling. A simple deployment script might look like this:
This gives you a single log file and still surfaces success or failure cleanly to the user.
Be Careful in Scheduled Tasks
When the same command runs from Task Scheduler, environment variables and working directories may differ from what you see interactively in cmd. Use absolute paths for logs and add start or end markers so you can line up the output with scheduler events.
That extra context saves time when you have to debug jobs after the fact.
Common Pitfalls
The most common mistake is reversing the order and writing 2>&1 > file, which does not combine the streams the way people expect. Another is forgetting to quote a log-file path that contains spaces. Teams also capture logs correctly but ignore errorlevel, which means failures are recorded but not acted on.
Summary
- Use
mycommand > file 2>&1to combine standard output and standard error into one file. - Use
>> file 2>&1when you want to append instead of overwrite. - Order matters because
cmdevaluates redirection from left to right. - Group command blocks when several commands should share one log file.
- Pair redirection with exit-code checks so the script both logs and responds to failures.

