Pcornel
Browse topics
Options
Recent
Favorites
Windows · Technical reference

Redirection

Direct answer

Redirection sends command output or input to files: > overwrites, >> appends, 2> redirects errors, 2>&1 merges errors with output, and < reads input from a file, such as dir > list.txt 2>&1.

When to use this

Use this when saving command output or errors to files.

First 30 seconds

1. > file overwrite, >> file append, 2> errors, 2>&1 both.

2. Order: cmd > out.txt 2>&1.

3. Discard: > nul.

4. Permission denied writing → run elevated or choose another folder.

5. Check the file content.

Operational verification

cmd.exe opens target files → handles redirected → command writes to them

Context & applicabilityCommon mistakes · platform/version boundaries
Common mistakes
Common mistakes
  • 2>&1 > file in the wrong order (stderr still goes to the console).
  • Overwriting log files with > instead of appending with >>.
  • PowerShell > writing UTF-16 in Windows PowerShell 5.1 (use Out-File -Encoding utf8).
Platform / version applicability
Windows execution, automation and observability
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformWindows execution, automation and observability
Scope / prerequisiteUse this when saving command output or errors to files.
What next?2 suggested next steps
Evidence1
On this pageJump to a specific section
Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • > overwrites a file, >> appends to it.
  • 2>&1 sends errors to the same place as output.
  • Redirection order matters: > file 2>&1 works, 2>&1 > file does not.
Key checks
  • echo %PATH% — PATH in CMD
  • set — environment variables
  • where <name> — command resolution
  • echo %ERRORLEVEL% — exit code
ReferenceWhy it matters1 section · depth on demand

Redirection operators

OperatorEffect
>Write stdout to a file (overwrite)
>>Append stdout
2>Write stderr
2>&1Send stderr to the same place as stdout
<Read stdin from a file
> nulDiscard output

Example: robocopy C:\src D:\dst /MIR > copy.log 2>&1.

ReferenceImportant terms & boundaries2 sections · depth on demand

Examples

ipconfig /all > net.txt 2>&1          :: stdout and stderr to file
robocopy src dst /MIR >> copy.log     :: append
command 2>nul                         :: discard errors
sort < names.txt

Documented example, not captured output. 2>&1 must come after the file redirection.

In depth

Redirection in cmd (also valid in PowerShell for native programs):

SyntaxEffect
> filestdout to file (overwrite)
>> fileAppend
2> filestderr to file
> file 2>&1Both to the same file
> NULDiscard
< fileRead stdin from file
robocopy C:\src D:\dst /E > C:\Logs\copy.log 2>&1
ping -n 1 srv01 > NUL && echo up || echo down

Order matters: 2>&1 > file sends stderr to the console, > file 2>&1 sends both to the file.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docWindows execution, automation and observability current

Authoritative/upstream documentation reviewed for bounded claims in Command Prompt → Redirection. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands · confidence 94%
Confirmation

Confirm action

Continue?