PowerShell Introductory Tutorials PowerShell Piping Introduction _powershell

Source: Internet
Author: User

Piping is a rotten thing for the shell, and it greatly improves the ability to program on the command line. Deep understanding and proficiency in the use of piping is PowerShell master of the road must pass the stage. Without pipelines, we had to save the intermediate results through a number of variables, although this is a trick for scripting and other compiled languages, but it's a very painful thing to do with a shell that fights on the command line.

Piping model

As the name suggests, a pipe is a pipe that connects things together to form a channel. On the surface, a pipe is a string of commands that are connected by a pipe symbol (|). From a functional standpoint, the pipe is like an assembly line, sending the result of the previous processing to the next process as input. Commands that are connected by pipes are executed from left to right, and the results are then output to the console. The previous pipes were sent either as strings, or as expressions that could be formatted as strings, and the PowerShell pipe stream could transfer objects.

How does the pipe in the PowerShell work?

PowerShell pipeline, able to pass objects, this is a great progress. By this time, the pipe has the flavor of a point function call. The caller is capable of passing complex objects, and the receiver is going to match the type of the object, and an error will occur when the incorrect command is executed. But there are exceptions here. Not only can the PowerShell pipeline be passed across the entire object, but it can also be passed by the property name, which greatly improves the usability of the pipeline and the processing power. This allows us to use pipelines to pass some of the properties of an object in a mismatched type.

Examples of pipe use

In PowerShell, there are some commands specifically designed for piping such as get-member, sort, measure, and so on, while some commands are not specifically designed for piping, but they are very glamorous in the pipeline. The following examples illustrate several of the most commonly used commands:

Select

Using the Select command in a pipeline is as common as using dir in cmd. Without looking at the detailed syntax of the SELECT command, you can write the most commonly used select method. As follows:

Copy Code code as follows:

Dir | Select Name,lastwritetime

This command displays the name and last modified date of the files and subfolders in the current folder.

where

The where command is used for filtering, which can be abbreviated to?, using the following example:

Copy Code code as follows:

Dir |? {$_.mode–like "d*"} | Select Name,lastwritetime

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

Foreach

foreach can be processed for each object passed over, and can be abbreviated to '% '. Use the example below:

Copy Code code as follows:

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

The result of this command is the output similar to "the size of file ' Myscript.ps1 ' is 1520" for information such as each file line.

Conclusion

The advantages of the pipeline are many, in this small space is endless. After the passage, slowly experience, you will know the beauty 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.