powershell max/first/aggregate functions

蹲街弑〆低调 提交于 2019-12-01 15:44:33

Use the Measure-Object command:

C:\PS> 2,1,3 | Measure-Object -Maximum


Count    : 3
Average  :
Sum      :
Maximum  : 3
Minimum  :
Property :

Or to get just the max:

C:\PS> (2,1,3 | Measure -Max).Maximum

There's also a Minimum, Average and Sum available as you can see from above. But you have to tell PowerShell to compute those with parameters to Measure-Object e.g. -Sum -Average etc.

Keith correctly mentions PowerShell equivalents to some LINQ extension methods. Note that you can call most extension methods from PowerShell, you just can't invoke them like they are members.

Extension methods are really just static methods with some extra rules in C# to allow them to be called like members.

PowerShell doesn't implement any of those rules, but that doesn't stop you from calling them as static methods, e.g.

[System.Linq.Enumerable]::Average([int[]](1..10))

$arr | foreach { if($max -lt $_ ) {$max = $_ }};$max

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!