Using Windows PowerShell as an IT Pro – Part 6
In my last post I talked about how to create a profile and add commands to it. Now I will talk about piping commands.One major advantage of using objects is that it makes it much easier to pipeline commands, that is, to pass the output of one command to another command as input. The cmdlet that receives an object can act directly on its properties and methods without any conversion or manipulation. Users can refer to properties and methods of the object by name, rather than calculating the position of the data in the output.
Pipelines act like a series of connected segments of pipe. Items moving along the pipeline pass through each segment. To create a pipeline in Windows PowerShell, you connect commands together with the pipe operator “|”. The output of each command is used as input to the next command.
Piping works virtually everywhere in Windows PowerShell. Although you see text on the screen, Windows PowerShell does not pipe text between commands. Instead, it pipes objects.
The notation used for pipelines is similar to that used in other shells, so at first glance, it may not be apparent that Windows PowerShell introduces something new. For example, if you use the Out-Host cmdlet to force a page-by-page display of output from another command, the output looks just like the normal text displayed on the screen, broken up into pages:
Get-ChildItem -Path C:\WINDOWS\System32 | Out-Host -Paging

The Out-Host -Paging command is a useful pipeline element whenever you have lengthy output that you would like to display slowly. It is especially useful if the operation is very CPU-intensive. Because processing is transferred to the Out-Host cmdlet when it has a complete page ready to display, cmdlets that precede it in the pipeline halt operation until the next page of output is available.
You will see the pipe used a lot in my future blog posts and get a better idea of how and where it can be used.
In my next post we will look at different formatting options in Windows PowerShell.
No comments:
Post a Comment