(17) Redirection operators in PowerShell

Source: Internet
Author: User

By default, Powershell sends the output to the screen display. However, PowerShell can also redirect the output to a text file, or redirect the error output to a regular output stream.

What is the use of the redirect operator?

The redirect operator means that we can output the command output to the specified file, fully satisfying the log requirement in the script, which can take advantage of the details of the redirected print script or command execution.

The following redirection operators are available in Powershell.

Operator Description Example
>
Sends the output to the specified file. PS d:\> get-process > Process.txt
>> Appends the output to the specified file. PS d:\> dir *.txt >> process.txt
2> Sends an error in the output to the specified file. PS d:\> get-process None 2> errors.txt
2>>
Appends an error in the output to the specified file. the operator is the most frequently used redirection operator in actual use. PS d:\> get-process None,powershell 2>>. \errore.txt
2>&1
Sends an error to the successful output stream. It is important to note that the command execution information is displayed, but the incorrect information is not displayed. PS d:\> get-process None,powershell 2>$1

The syntax for the redirect operator is as follows:

<input> <operator> [<path>\]<file>

Note that if the specified file already exists, the redirect operator (> and 2>) that does not append the data will overwrite the current contents of the file without warning . However, if the file is read-only, hidden, or system files, the redirection will fail. The Append redirection operator (>> and 2>>) does not write to read-only files, but appends content to the system or hidden files.


It is also important to note that the redirect operator is just a syntactic sugar provided by Powershell (it is easy to use, but also more abstract). For example:

PS d:\> get-process > Process.txt

is equivalent to the following command.

PS d:\> get-process | Out-file Process.txt

mention Out-file has to mention the following PowerShell redirection encoding problem.

Powershell redirection operator > What encoding is used when redirecting files? Is it ASCII or Unicode? The answer is that it depends on the redirect operator > on the right is the file or program.

The String type in Powershell uses a 16-bit Unicode encoding, which is an instance of the System.String class in the. Net Framework. So when you use the redirect operator in PowerShell, the Unicode encoding is used. Unicode encoding is also the default encoding for out-file command redirection files.

If the redirection operator > is a program, not a file, the encoding used depends on the value of the PowerShell built-in variable $OutputEncoding, and the default value for $OutputEncoding is ASCII ( Many programs do not correctly handle Unicode encoding, which is a relatively complex point of knowledge in a computer, and it is interesting to know the difference between the next two encodings. Here are the values $OutputEncoding in My computer.

ps d:\>  $OutputEncodingIsSingleByte        : TrueBodyName          :  us-asciiencodingname      : us-asciiheadername         : us-asciiWebName            : us-asciiWindowsCodePage   : 1252IsBrowserDisplay  :  Falseisbrowsersave     : falseismailnewsdisplay : trueismailnewssave     : TrueEncoderFallback   :  system.text.encoderreplacementfallbackdecoderfallback   :  system.text.decoderreplacementfallbackisreadonly        :  truecodepage          : 20127 

Of course this value can be changed, using the example of Powershell designer Jeffrey blog https://blogs.msdn.microsoft.com/powershell/2006/12/11/ outputencoding-to-the-rescue/


So when redirecting the output of PowerShell, you can use the syntax sugar (>), or you can use the Out-file command, the advantage of using Out-file is that you can specify the encoding format with the-encoding parameter.

It is also important to note that you can use the Out-file cmdlet with the Force parameter if you are forcing content to be redirected to read-only, hidden, or system files.

what exactly does 1, 2 in the redirect operator mean?

Numbers in the redirect operator The type of output information represented
1 Output/success
2 Error (Fault message)
3 Warning (Warning message)
4 Verbose (More information)
5 Debug (Debug information)

The redirection operator in the table above is a redirection of the kind of output information that is supported in all versions of Powershell. The following redirection operators are also supported in Powershell 3.0+.

Operator Description
3>
Redirects the warning message to the specified file.
4>>
Appends the details of the output to the specified file.
5>&1
Outputs debug information to a success stream. It is important to note that the command execution information is displayed and the debug information is not displayed.
*> Redirect All information (5 listed above) to the specified file.

The use of these operators is exactly the same as the use of the operators in the previous table. Not listed here.


This article from "Flower Blossom Fall" blog, declined reprint!

(in) the redirection operator in PowerShell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.