Network Administration: Windows Command Redirection and Piping
Redirection and piping of Windows commands are related techniques. Redirection lets you specify an alternative destination for output that will be displayed by a command or an alternative source for input that should be fed into a command. For example, you can save the results of an ipconfig /all command to a file named myconfig.txt like this:
C:>ipconfig /all > myconfig.txt
Here, the greater-than sign (>) is used to redirect the command’s console output.
If a command accepts input from the keyboard, you can use input redirection to specify a file that contains the input you want to feed to the command. For example, you can create a text file named lookup.txt with subcommands for a command such as nslookup. Then, you can feed those scripted subcommands to the nslookup command, like this:
C:>nslookup < lookup.txt
Piping is a similar technique. It takes the console output from one command and feeds it into the next command as input. Piping is often used with special commands called filters, which are designed to read input from the console, modify the data in some way, and then write it to the console.
For example, suppose that you want to display the contents of a file named users.txt sorted into alphabetical order. You can use the Type command, which displays a file on the console, and then you can pipe the output into the Sort command, a filter that sorts its input and displays the sorted output on the console. The resulting command would look like this:
C:>type users.txt | sort
The vertical bar is often called the pipe character because it’s the symbol used to indicate piping.