2

I would like to build powershell pipeline like

cmd | transform_a | stdout_and | transform_b | store_variable ^ | copy input to next consumer and to console 

I tried to utilize Tee-Object but without success. I dont want to do this

dir | select -last 5 | tee lastFiveLines | select -first 1 echo $lastFiveLines 

altough it works. Instead I want the content to be printed directly.

1 Answer 1

2

You can try a foreach-loop and Out-Default or Out-Host to skip the rest of the pipeline (Host is Default output anyways), while also sending the object down the pipeline. Sample:

Get-ChildItem | Select-Object Name, FullName | ForEach-Object { #Send Name-value directly to console (default output) $_.Name | Out-Default #Send original object down the pipeline $_ } | Select-Object -ExpandProperty FullName | % { Start-sleep -Seconds 1; "Hello $_" } 

You can create a filter to make it easy to reuse it.

#Bad filter-name, but fits the question. filter stdout_and { #Send Name-value directly to console (default output) $_.Name | Out-Default #Send original object down the pipeline $_ } Get-ChildItem | Select-Object Name, FullName | stdout_and | Select-Object -ExpandProperty FullName | % { Start-sleep -Seconds 1; "Hello $_" } 
Sign up to request clarification or add additional context in comments.

2 Comments

So, no beautiful one-liner for the task?
You can remove the linebreaks in the foreach.. ForEach-Object { $_.Name | Out-Default; $_ }, or create a filter (see updated answer).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.