Windows PowerShell Using Pipelines _powershell

Source: Internet
Author: User

The pipeline is not a new thing, the previous CMD console also has redirected commands, such as Dir | More can display the results in a separate screen.
The traditional cmd pipeline is text-based, but PowerShell is based on objects.

Ps> ls | Sort-object-descending Name | Select-object Name,length,lastwritetime | convertto-html | Out-file ls.html

ps> get-content. ls.html

Name Length lastwritetime
test.vbs 170 2011/11/28 16:42:03
test.txt 242 2011/11/23 17:37:37
TEST.PS1 140 2011/12/2 18:47:35
psdrive.html 2556 2011/11/30 16:04:00
powershell_cmdlets.html 735892 2011/11/24 17:44:37
ping.bat 2011/11/28 15:30:04
name.html 7420 2011/11/24
17:37:28 myscript 2011/11/29 18:21:28 ls.html 434 2011/12/14 11:22:30
logotestconfig.xml 186 2011/11/28 11:12:08
function.ps1 21466 2011/11/29 19:23:58
employee.xml 556 2011/11/25 11:20:33
d.txt 0 2011/11/23 17:25:23
c.txt 0 2011/11/23 17:25:23< C15/>b.txt 0 2011/11/23 17:25:23
alias.ps1 12060 2011/11/24 20:27:24
alias 12060 2011/11/24 20:26:36
ABC 2011/11/23 17:25:53
a.txt 26384 2011/11/24 20:04:31
a.html 67580 2011/11/24 18:30:13

Lists the directories and files in the current directory, and then sorts the file name, file size, file modification time, converted to HTML format, and output to the current directory ls.html

Object-oriented pipeline

The example above belongs to an object-oriented pipeline, and the end of each command can be further processed with the result of the previous command, unless the pipeline ends with an output command. Just like Sort-object, sort the list of files, you need to tell them the sort of keyword, in ascending or descending order. The return value of LS is an array, and each element in the array is an object, and each attribute of the object can be used as the Sort-object sort key. However, you must specify a specific keyword when sorting, because the objects passed by PowerShell may have many properties. Unlike ordinary text, object information is structured so that the PowerShell pipeline becomes more powerful and convenient.

The result of the conversion command execution is text

When the PowerShell command is executed, the interpreter defaults to appending a pipe command at the end of the command, Out-default, so that the original object result can be displayed as text on the console, but the result is not converted. You can continue to manipulate the results of the object using other pipes, but once you use a command such as convertto-html, the result is converted to plain text in a fixed format.

Common commands for further processing of pipeline results are:

Compare-object: Compares two groups of objects.
Convertto-html: Converts the Microsoft. NET Framework object to Html that can be displayed in a Web browser.
Export-clixml: Creates an xml-based representation of an object and stores it in a file.
Export-csv: Converts the Microsoft. NET Framework objects into a series of comma-delimited, variable-length (CSV) strings, and saves those strings to
In a CSV file.
Foreach-object: Performs actions on each set of input objects.
Format-list: Formats the output as a list of properties, each of which is displayed in one row.
Format-table: Formats the output as a table.
Format-wide: Sets the object's format to a wide table that can display only one property of each object.
Get-unique: Returns a unique item from the sorted list.
Group-object: The specified property contains a group object with the same value.
Import-clixml: Import the Clixml file and create the corresponding object in Windows PowerShell.
Measure-object: Calculates the numeric properties of an object and the number of characters, words, and lines in a string object, such as a text file.
MORE: Display the results in a separate screen.
Out-file: Sends output to a file.
Out-null: Deletes the output and does not send it to the console.
Out-printer: Sends the output to the printer.
Out-string: Sends the object as a column string to the host.
Select-object: Select the specified property of an object or a group of objects. It can also select a unique object from an array of objects, or you can choose from the beginning or end of an array of objects
Select the specified number of objects.
Sort-object: Sorted by property value object.
Tee-object: Save the command output in a file or variable and display it in the console.
Where-object: Creates a filter that controls which objects are passed along the command pipe.

Handling mode of Pipelines

When we combine many names into a single pipe, we might be interested in the execution of each command in sequential or simultaneous execution? The results of the pipeline processing are actually real time. This is why there are two pipe modes:
Sequential mode (slower): In sequential mode, a single command is executed at the same time in the pipeline, and all results are delivered to the next command only if all of the current command is executed. This pattern is slow and memory-intensive, as it is necessary to allocate space to store intermediate results many times.
Streaming mode (faster): Stream mode executes all commands immediately and may execute multiple commands at the same time. The previous command may produce multiple results, but once one of the results is produced, it is immediately delivered to the next command. Such a streaming pattern saves memory, and a certain task in the pipeline is still executing, but some of the results have been exported. Reduces the retention of intermediate results.

Blocking of PIPE commands

You can use Sort-object to sort the results of a pipe, but sometimes sorting can cause the entire operating system to block because the execution of the sort command is in sequential mode, and the result of the previous command must be complete to sort.
So when using such commands, pay attention to the size of the action objects and the memory they need. For example this command:
Dir C:-recurse | Sort-object
The-recurse option is to recursively query subdirectories, and imagine how large the system disk files and directories are. Once this command has been run, it will take a long time and may even cause the system to crash and reboot the computer. When you execute this command, you can open Task Manager to see that the memory footprint of the PowerShell process increases at a rate of dozens of megabytes per second.
Which commands may be blocked, depending on how the command is implemented and the size of the object being processed, for example, sort-object causes blocking is certainly due to the technical implementation of the use of internal ordering, not using the outside sort. However, commands such as out-host-paging are in the outflow mode and generally do not cause the system to block.

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.