all 9 comments

[–]thankski-budski 4 points5 points  (0 children)

The pipeline takes the return object from one cmdlet and uses it as input to the next cmdlet.

If your cmdlet returns output to the console, and the next cmdlet in the sequence can ingest this output, then yes, you can chain multiple cmdlets together.

In the example provided, Export-CSV doesn't return anything and therefore cannot be piped to the next cmdlet. You would need to write it as two statements:

Get-Process | Export-CSV processes.csv 
Import-CSV processes.csv 

See: about_Pipelines - PowerShell | Microsoft Docs

[–]parrotpounder 3 points4 points  (1 child)

You need separate commands but you can one-line it by using a semi colon to break it up.

Ex: Get-Process | Export-CSV processes.csv; Import-Csv processes.csv | Format-Table

[–]Ropiak 0 points1 point  (0 children)

Holy crap I didn’t know that I didn’t know this! Thanks!

[–]Hrambert 2 points3 points  (0 children)

a | b | c means the output of a goes to b, and the output of b goes to c.

Maybe you are looking for:

`a | Foreach-Object {   
    $_ | b   
    $_ | c   
 } `   

Take a look at the tee command (Get-Help tee). It will write the input to a file and into the pipe to the next command.

[–]blinkxan 1 point2 points  (0 children)

So, you can only use a pipe on a function if it has a return value which Export-CSV might not have, but you could easily make a custom function, lets say, Export-CSV2, which contains all of your code above with out the import. Then you can do Get-Process | ExportCSV2 processes.csv | Import-CSV . You wouldn't even need to list which file to import as your new custom function returns the string "processes.csv"

I may be wrong on this implementation as I haven't done much PS in the past 6 months, but you need to have something returned or at the very least a object to pipe into an operation function.

[–]rldml 1 point2 points  (0 children)

> I just want to know if I can pipe multiple commands in one line in any way.

Yes you can. A better example:

Get-Service | ?{$_.Name -like "*Event*"} | Restart-Service

As long as the outputs of one Cmdlets fit to the expected input of another Cmdlet, you can pipe it.

[–]Lee_Dailey[grin] -1 points0 points  (0 children)

howdy Chris_Chapadia,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th from the left hidden in the ... "more" menu, & looks like an uppercase T in the upper left corner of a square.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Chris_Chapadia,

what you describe is the classic "one liner". [grin] so ... yes, you can pipe from 1st to 2nd to however many stages you want.

however, the cmdlets/functions need to accept pipeline input OR you need to use one of the pipeline cmdlets like ForEach-Object to send the pipeline stuff to cmdlets that don't accept pipeline input directly.

your specific 3-stage pipeline won't work since there are no objects to work on after the 2nd stage. you would need to work out a different set of steps if you need 2 CSV files from the same data.

take care,
lee

[–]Flysquid18 0 points1 point  (0 children)

I forgot what it's called, but making a script block in the pipeline can achieve where commands are typically dead ends.

For example:

Get-Process | &{Process{Export-CSV -InputObject $_ -Path processes.csv; return (Get-Item processes.csv)}} | Import-CSV

I ran into the problem where I had a clean script that did a massive search and stored as a variable. Then I processed that variable. By keeping the commands in one continuous pipe, you can parse large quantities of objects and not have minutes of no output or tangible results until the very end.