pipes and foreach loops

后端 未结 2 1311
一个人的身影
一个人的身影 2020-12-21 15:44

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

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-21 16:06

    You need to evaluate the foreach before piping the resulting Object like you did in the first test:

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

    Alternatively, use the % shorthand to which will evaluate it before piping it as well:

    gci | % { $_.Length } | measure -max
    

提交回复
热议问题