PowerShell command Output Control Method

Source: Internet
Author: User

When writing Production Quality functions, 2/3 of previous articles on Windows PowerShell introduced input and error handling. Now let's take a look at the last factor: output.

The output of scripts and commands has been cumbersome and inconsistent, even in the same tool. Users do not know much about the awk, sed, or grep tools we have been using these days, and provide the desired data around the output changes. A main tenant of Windows PowerShell is to have the expected output type and avoid the need to parse text. So how does PowerShell handle it? The answer is yes, as I explained in my previous article, what makes Windows PowerShell so special?

Before learning how PowerShell provides cmdlets for writing data, it is important to understand how PowerShell processes the output. Most shells have a concept of data flow. The most common ones are stdout and stderr. PowerShell has a similar concept, which we call pipeline.

In PowerShell, there are three basic channels for data:

PowerShell has many lelets for writing data, mainly including:

Write-Host-this is a simple cmdlet. It writes data directly through the pipeline and sends it to the console. It is useful if you do not want to provide information about redundant pipelines to users.

Write-Output-it is used to directly Write the specified data to the pipeline. I am talking about 'designate 'because if you do not specify any value, the default value will be written to the pipeline.

Write lelets in other ways:

As you know, there are many ways to provide feedback to users. The key to success is to use your tools correctly. For example, you plan to use write-host instead of write-output to display messages to users. Why? Because you do not want to break your tagged output data in the user, it is very important to maintain data integrity according to the user's expected goals. With pipelines without redundant information, you can access data that is consistent with the expected attributes without parsing the data.

In other words, a pipeline without redundant information can make users more happy.

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.