Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does the pipe | mean in PowerShell?

Tags:

powershell

For instance:

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

What exactly is happening here?

like image 782
Shaun Luttin Avatar asked Jan 09 '14 18:01

Shaun Luttin


People also ask

What does the vertical bar do in PowerShell?

In PowerShell, the vertical bar ( | ) is the pipe symbol. This tells PowerShell that you want to take the output of one command and pass it as the input (or pipe it) to the next command.

What is pipeline variable in PowerShell?

Windows PowerShell has a unique pipeline variable $_ or $PSItem . Windows PowerShell's ability to pipe entire objects from one command to another is needed to represent that object traversing the pipeline. When one Windows PowerShell cmdlet pipes something to another command, that cmdlet can send an object.


1 Answers

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.

like image 167
Anthony Neace Avatar answered Nov 15 '22 06:11

Anthony Neace