1

Let's say I have multiple non-numerical arrays in PowerShell:

$a = (a, b, c, d)             # $a.count equals 4 items
$b = (e, f, g, h, i, j)       # $b.count equals 6 items, which is the highest count of items in one of the arrays
$c = (k, l, m, n, o)          # $c.count equals 5 items
$d = (p, q)                   # $d.count equals 2 items
...

After declaring all those arrays, I would like to get the highest number of counts from all of them, which in the case above would be a count of 6 from array $b. Is there an easy way to achieve this, instead of comparing each array against the next one and check if the count is higher than before?

1
  • 1
    Do you want to get only the highest count? or the array is also needed $b Commented Oct 8, 2021 at 8:08

1 Answer 1

2

You can use Measure-Object Cmdlet with -Property and -Maximum Parameters. The -Property parameter allows you to measure based on the property value(In this case it's based on the array Count property)

$a = ('a', 'b', 'c', 'd')
$b = ('e', 'f', 'g', 'h', 'i', 'j')
$measureInfo = ($a, $b) | Measure-Object -Property Count -Maximum
Write-Output $measureInfo.Maximum # This will print 6

Note that this will print only the maximum Count. If you want the array also, you probably have to apply a filter based on this value.

$MaxArray = ($a, $b) | Where-Object {$_.Count -eq $maximumCount.Maximum}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your solution was correct and exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.