PowerShell getting started (7): pipelines-programming on the command line

Source: Internet
Author: User

The pipeline is a magic thing for Shell, and it greatly improves the programming capability on the command line. Understanding and mastering the use of pipelines is an essential step for PowerShell masters. Without pipelines, we have to use many variables to save intermediate results. Although this is a common trick for scripts and other compiled languages, for Shell fighting on the command line, it is a very painful thing.

MPs Queue Model

As the name suggests, a pipe connects things to form a channel. On the surface, pipelines are a string of commands connected by pipeline symbols |. In terms of functions, the pipeline is like a pipeline, which transmits the result of the previous processing to the next processing as the input. Commands connected by pipelines are executed from left to right, and the results are output to the console. In the past, pipeline streams were transmitted using strings or expressions that can be formatted as strings. Pipeline streams of PowerShell can transmit objects.

How does the pipeline work in PowerShell?

PowerShell pipeline, which can pass objects, is a huge improvement. At this time, the pipeline has the taste of function call. If the caller can pass complex objects, the receiver must match the object type, and an error occurs when an incorrect matching command is executed. But there are exceptions here. The PowerShell pipeline can be passed not only by the entire object, but also by the attribute name. This feature greatly improves the availability and processing capability of the pipeline. This allows us to use pipelines to pass partial attributes of objects even if the types do not match.

Pipeline example

In PowerShell, there are some commands specially designed for pipelines, such as get-member, sort, and measure. Some commands, although not specifically designed for pipelines, are brilliant in the pipeline. The following are some of the most common commands:

Select

Using the select command in the pipeline is as common as using dir in Cmd. You can write out the most commonly used select method without looking at the detailed syntax of the select command. As follows:

dir | select name,LastWriteTime

This command displays the names and last modification dates of files and subfolders in the current folder.

Where

Which of the following can be abbreviated ?, Example:

dir | ? {$_.mode –like “d*”} | select name,LastWriteTime

This command only displays the subfolders of the current folder, not the files.

Foreach

Foreach can process each passed object, which can be abbreviated as '% '. Example:

dir | ? {$_.mode -notlike "d*"} | % {echo "The size of file '$($_.name)' is $_.Length"}

The result of this command is output information similar to "The size of file 'myscript. ps1' is 1520". Each file has a row.

Conclusion

Pipeline has many advantages, which cannot be completed in this small space. After going on to the road, you will know the highlights of the pipeline.

Related Article

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.