Windows command output and redirection examples.

---
title: Windows Command Output and Redirection
author: SS64
date: April 4, 2018
source: https://ss64.com/nt/syntax-redirection.html
notoc: false
---

## Redirecting Command Output

### Examples

#### Redirect Command Output to File (overwrite)

    command > filename

#### Append Command Output to File

    command >> filename

#### Pass the Contents of File as stdin to Another Command

    command < filename

#### Pipe Output from One Command as Input to Another

    commandA | commandB

#### Call Two Commands to Execute, One after the Other

    commandA & commandB

#### Run Command ONLY if the Previous Command SUCCEEDS

    commandA && commandB

#### Run Command ONLY if the Previous Command FAILS

    commandA || commandB

#### If commandA succeeds run commandB, if it fails commandC

    commandA && commandB || commandC

Success and failure are based on the Exit Code of the command. In most cases,
the Exit Code is the same as the [ErrorLevel](https://ss64.com/nt/errorlevel.html).

## Numeric Handles

|           Name           |    Description    | Integer value(s) |
|--------------------------|-------------------|------------------|
| Standard input (stdin)   | Keyboard input    | 0                |
| Standard output (stdout) | Text output       | 1                |
| Standard error (stderr)  | Error text output | 2                |
| UNDEFINED                | Unknown           | 3-9              |

### Examples

|          Example           |                 Description                  |
|----------------------------|----------------------------------------------|
| `command 2> filename`      | Redirect any error message into a file       |
| `command 2>> filename`     | Append any error message into a file         |
| `(command)2> filename`     | Redirect any CMD.exe error into a file       |
| `command > file 2>&1`      | Redirect errors and output to one file       |
| `command > fileA 2> fileB` | Redirect output and errors to separate files |

## Redirecting to NUL (hide errors)

### Examples

|           Example           |                     Description                     |
|-----------------------------|-----------------------------------------------------|
| `command 2> nul`            | Redirect error messages to NUL                      |
| `command >nul 2>&1`         | Redirect error and output to NUL                    |
| `command >filename 2> nul`  | Redirect output to file but suppress error          |
| `(command)>filename 2> nul` | Redirect output to file but suppress CMD.exe errors |

## Tips

- Any long filenames must be surrounded in "double quotes".
- A CMD error is an error raised by the command processor itself rather than the program/command.
- Redirection with `>` or `2>` will overwrite any existing file.
- You can also redirect to a printer with `> PRN` or `>LPT1`.