5

For instance:

get-service | export-csv C:\services.csv

What exactly is happening here?

1

2 Answers 2

10

Quick Answer

The output object of Get-Service becomes the input of a parameter of Stop-Service that accepts pipeline input.

Details

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.

1
  • +1 for thoroughness, i.e. talking about 1. output-input object compatibility, 2. handling input collections one-at-a-time via streaming, and 3. using get-help to find parameters that accept pipeline input. Commented Jan 9, 2014 at 19:17
2

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.

2
  • What does it mean to normalize object data? Commented Jan 9, 2014 at 19:12
  • 1
    "Normalizing", in this context, means "unrolling" arrays and collections into a stream of individual objects for input, and at the end of the pipeline packaging multiple objects into an array if it's being directed to a variable.
    – mjolinor
    Commented Jan 9, 2014 at 19:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.