For instance:
get-service | export-csv C:\services.csv
What exactly is happening here?
The output object of Get-Service
becomes the input of a parameter of Stop-Service
that accepts pipeline input.
In the case of stop-service, this parameter is -InputObject
, which accepts a ServiceController
object or objects (ServiceController[]
). Since Get-Service
outputs that type of object, you can use the pipeline to pass it into Stop-Service
. If there is a collection of ServiceController
objects, Stop-Service
would be called once for each item in the collection in a process known as streaming.
When items are streaming on the pipeline, they move on as soon as they are available. If you would like to wait until the collection has been completed to pass it, you can wrap the input in a set of parenthesis.
Finding parameters that accept pipe input
If you're ever using Get-Help
to find out about a particular cmdlet, you can scroll to the parameters section to see which parameter accepts pipeline input:
PS C:\> Get-Help Stop-Service -full
...
-InputObject <ServiceController[]>
Required? true
Position? 0
Accept pipeline input? true (ByValue)
Parameter set name InputObject
Aliases None
Dynamic? false
-Name <string[]>
Required? true
Position? 0
Accept pipeline input? true (ByValue, ByPropertyName)
Parameter set name Default
Aliases ServiceName
Dynamic? false
So by viewing the documentation for that cmdlet, we see that you can use a collection of ServiceController objects or even the string name of service as input over the pipe. Here's an example using the related cmdlet Get-Service:
#Using a string
PS C:\Users\2.34> "MySQL" | Get-Service
Status Name DisplayName
------ ---- -----------
Running MySQL MySQL
#Using a ServiceController object
PS C:\> Get-Service "MySQL" | Get-Service
Status Name DisplayName
------ ---- -----------
Running MySQL MySQL
More reading
If you're interested in learning about pipeline input by property name, there's a great TechNet article here. You can find the about_Pipelines
documentation here.
There's a chapter in the Powershell Owner's Manual on the pipeline that may be helpful:
http://technet.microsoft.com/en-us/library/ee176927.aspx
Basically the pipeline is a mechanism for normalizing object data and passing it between cmdlets in a controlled manner, so that each cmdlet can tell the previous cmdlet or filter/function in the pipeline when it is ready for more data.