ereflexer
ereflexer

Reputation: 13

When pipeline and foreach create value?

I'm waiting that response of this expression 1..2 | foreach { echo $_; $_ } | foreach { $_*$_ } be like

1
1
2
4

but receive

1
1
4
4

So i dont understand why is this happends.

Upvotes: 1

Views: 79

Answers (1)

js2010
js2010

Reputation: 27453

Echo sends its output to the 2nd foreach. I think you want to use write-host, which doesn't send its output to the rest of the pipeline. Echo would do the same thing in cmd or bash.

1..2 | foreach { write-host $_; $_ } | foreach { $_*$_ }

1
1
2
4

Upvotes: 1

Related Questions