tubos e laços foreach

Recentemente, eu brinquei com o PowerShell e notei um comportamento estranho ao usar pipes eforeach loops que eu não conseguia entender.

Este código simples funciona:

$x = foreach ($i in gci){$i.length}
$x | measure -max

Faz sentido.

Mas esse código não:

foreach ($i in gci){$i.length} | measure -max

E estou recebendo o seguinte erro:

An empty pipe element is not allowed.
At line:1 char:33
+ foreach ($i in gci){$i.length} | <<<<  measure -max
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : EmptyPipeElement

Qual é a diferença entre esses dois métodos e por que o segundo falha?

questionAnswers(2)

yourAnswerToTheQuestion