How to use pipes and redirection
Overview
A pipe, |, sends one command's normal output into another command. Redirection writes output to a file or reads input from one.
Use > to replace, >> to append, and < for input. Test the producing command alone before redirecting it.
- Normal output is stream 1 and error output is stream 2.
2>&1combines errors with normal output.- Use
^to escape a special character when CMD should treat it as text.
Commands to try
Type each command at the prompt, then press Enter. Replace sample names and paths with ones that exist on your computer.
Commands are generally not case-sensitive, but preserving the spelling shown here makes scripts easier to read.
dir /b | findstr /i ".txt"
systeminfo | more
ipconfig > network.txt
ping example.com >> network.txt
findstr /i "error" < app.log
somecommand > output.txt 2> errors.txt
Useful options and details
Options change how a command behaves. Add spaces exactly as shown, and use the command's built-in help when an option is unfamiliar.
A path containing spaces must be enclosed in double quotes, such as "C:\My Files".
>truncates an existing file before writing.>>adds to the end and can grow logs indefinitely.command1 && command2runs the second only if the first reports success.
Tips and common mistakes
Check the current folder and read the command output before assuming an operation succeeded.
Practice with disposable files first. Commands that delete, overwrite, or stop a process may not offer an Undo button.
- A typo after
>can overwrite the wrong file without warning. - Not every program uses error levels or output streams consistently.
- Pipes operate on text output, not the files merely named in that output.
Safe practice
Create a temporary practice folder under your user profile so the examples cannot affect Windows system files.
After each command, inspect the result with dir or another read-only command before continuing.
- Filter a bare directory list for text filenames.
- Redirect a harmless command to a new practice file.
- Append another line, then inspect the complete file.