Which of the following operators is used to redirect the output of a command to a file?
Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
about_Redirection
In this articleShort descriptionExplains how to redirect output from PowerShell to text files. Long descriptionBy default, PowerShell sends output to the PowerShell host. Usually this is the console application. However, you can redirect the output to a text file and you can redirect error output to the regular output stream. You can use the following methods to redirect output:
For more information about streams, see about_Output_Streams. Redirectable output streamsPowerShell supports redirection of the following output streams.
There is also a Progress stream in PowerShell, but it does not support redirection. Important The Success and Error streams are similar to the stdout and stderr streams of other shells. However, stdin is not connected to the PowerShell pipeline for input. PowerShell redirection operatorsThe PowerShell redirection operators are as follows,
where
Note Unlike some Unix shells, you can only redirect other streams to the Success stream. ExamplesExample 1: Redirect errors and output to a fileThis example runs
It uses Example 2: Send all Success stream data to a fileThis example sends all Success stream data to a file called
Example 3: Send Success, Warning, and Error streams to a fileThis example shows how you can combine redirection operators to achieve a desired result.
Example 4: Redirect all streams to a fileThis example sends all streams output from a script called
Example 5: Suppress all Write-Host and Information stream dataThis example suppresses all information stream data. To read more about Information stream cmdlets, see Write-Host and Write-Information
Example 6: Show the effect of Action PreferencesAction Preference variables and parameters can change what gets written to a particular stream. The script in this example shows how the value of
When we run this script we get prompted when
When we examine the log file we see the following:
NotesThe redirection operators that do not append data ( However, if the file is a read-only, hidden, or system file, the redirection fails. The append redirection operators ( To force the redirection of content to a read-only, hidden, or system file, use the When you are writing to files, the redirection
operators use Width of output when writing to a fileWhen you are writing to a file using either
Considering that the console width may be set arbitrarily on systems where your script is run, you may prefer that PowerShell format table output to files based on a width that you specify instead. The
Increasing the output width will increase memory consumption when logging table formatted output. If you are logging a lot of tabular data to file and you know you can get by with a smaller width, use the smaller width. In some cases, such as
For more information about Potential confusion with comparison operatorsThe Depending on the objects being compared, the output using
However, a check of the local filesystem can see
that a file called
Attempting to use the reverse comparison
If numeric comparison is the required operation, See also
FeedbackSubmit and view feedback for What is the operator to redirect output?This is known as redirecting output. Redirection is done using either the ">" (greater-than symbol), or using the "|" (pipe) operator which sends the standard output of one command to another command as standard input.
Which operators are called redirection operators?So, what we learned is, the “>” is the output redirection operator used for overwriting files that already exist in the directory. While, the “>>” is an output operator as well, but, it appends the data of an existing file. Often, both of these operators are used together to modify files in Linux.
Which characters can be used to redirect the standard output of a command to a file?Redirecting Standard Output
When the notation > filename is added to the end of a command, the output of the command is written to the specified file name. The > symbol is known as the output redirection operator.
How do you redirect the output of a command to a file in Linux?Redirection with >. command > file : Sends standard output to command 2> file : Sends error output to command 2>&1 : Sends error output to standard output.. command > file 2>&1 : Sends standard output and the error output to a file.. command &> file : Sends standard output and the error output to a file.. |