Use this when saving command output or errors to files.
Redirection
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.
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.
cmd.exe opens target files → handles redirected → command writes to them
Context & applicabilityCommon mistakes · platform/version boundaries
2>&1 > filein 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 (useOut-File -Encoding utf8).
Operational reference
Fast context before you go deeper.
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.
- > 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.
echo %PATH%— PATH in CMDset— environment variableswhere <name>— command resolutionecho %ERRORLEVEL%— exit code
ReferenceWhy it matters1 section · depth on demand
Redirection operators
| Operator | Effect |
|---|---|
> | Write stdout to a file (overwrite) |
>> | Append stdout |
2> | Write stderr |
2>&1 | Send stderr to the same place as stdout |
< | Read stdin from a file |
> nul | Discard 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):
| Syntax | Effect |
|---|---|
> file | stdout to file (overwrite) |
>> file | Append |
2> file | stderr to file |
> file 2>&1 | Both to the same file |
> NUL | Discard |
< file | Read 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.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
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%