Windows cmd
Standard Output
Standard Error
File Redirection
Command Line Tricks

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:

  1. stream 1 for standard output
  2. stream 2 for standard error

Common operators are:

  1. > to overwrite a file with standard output
  2. >> to append output to a file
  3. 2> to redirect only standard error
  4. 2>&1 to 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:

cmd
mycommand > output.log 2>&1

This works because:

  1. > output.log sends stream 1 to the file
  2. 2>&1 then sends stream 2 to the same destination as stream 1

If you want append mode instead of overwrite mode, use:

cmd
mycommand >> output.log 2>&1

That is the most common pattern for batch-job logs and scheduled task output.

Why Order Matters

These two commands are not equivalent:

cmd
mycommand > output.log 2>&1
mycommand 2>&1 > output.log

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.

cmd
1(
2  echo Start build
3  dir missing-folder
4  echo End build
5) > build.log 2>&1

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:

cmd
mycommand 1>nul 2> errors.log

To discard both normal output and errors:

cmd
mycommand >nul 2>&1

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:

cmd
1@echo off
2set LOG=%~dp0deploy.log
3
4echo [%date% %time%] deployment start > "%LOG%"
5(
6  git fetch origin
7  git switch main
8  git pull origin main
9  call build.bat
10) >> "%LOG%" 2>&1
11
12if errorlevel 1 (
13  echo Deployment failed. Check "%LOG%"
14  exit /b 1
15)
16
17echo Deployment succeeded. Log written to "%LOG%"

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>&1 to combine standard output and standard error into one file.
  • Use >> file 2>&1 when you want to append instead of overwrite.
  • Order matters because cmd evaluates 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.

Course illustration
Course illustration

All Rights Reserved.