19.5. Redirection
Windows PowerShell has two redirection operators, > and >>. A redirection operator redirects the output of a command (or pipeline) to a specified location. The > operator creates a new file and redirects text to it or, if the file exists, it overwrites the existing content. The >> operator appends text to an existing file without overwriting the existing content.
You can redirect text from the command line to a file. For example, to redirect the literal text Hello world! to a not yet existent file NonExistent.txt in the same directory use this command:
"Hello world!" > NonExistent.txt
You can check that the content has been added to the newly created file using the command:
get-content NonExistent.txt
If you then redirect new text to the file using the > operator, it overwrites the existing content, as you can demonstrate using the following commands:
"This overwrites the old text." > NonExistent.txt get-content NonExistent.txt
But, if you use the >> operator, you can append text to the file, as you can demonstrate using the following commands:
"This appends a new line to the file." > NonExistent.txt get-content NonExistent.txt
Figure 19-20 shows the results of executing the preceding commands in this section.
Figure 19.20. Figure 19-20
Similarly, you can use the >> redirection operator to append text to an existing file, as shown in Figure 19-20 when you execute ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access