Recently, I've been playing with PowerShell, and I've noticed some weird behavior when using pipes and foreach
loops that I couldn't understand.
This simple code works:
$x = foreach ($i in gci){$i.length}
$x | measure -max
Makes sense.
But this code won't:
foreach ($i in gci){$i.length} | measure -max
And I'm getting the following error:
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
What is the difference between those two methods, and why does the second one fails?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…