PowerTip: Find the Largest Number in a PowerShell Array

PowerTip: Find the Largest Number in a PowerShell Array

  • Comments 4
  • Likes

Summary: Easily find the largest number in a Windows PowerShell array.

Hey, Scripting Guy! Question How can I use Windows PowerShell to easily find the largest number in an array of numbers?

Hey, Scripting Guy! Answer Pipe the array to the Measure-Object cmdlet, and use the –Maximum switch:

PS C:\> $a = [array]1,2,5,6,3,2,9,1

PS C:\> $a | measure -Maximum

 

Count    : 8

Average  :

Sum      :

Maximum  : 9

Minimum  :

Property :

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>Hmmm...</p> <p>This is odd... &nbsp;Thisi does not work as expected.</p> <p>See:</p> <p> $a = [array]1,2,5,6,3,2,9,1</p> <p> $a|%{$_.GetType()}</p> <p>IsPublic IsSerial Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BaseType</p> <p>-------- -------- ---- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --------</p> <p>True &nbsp; &nbsp; True &nbsp; &nbsp; Object[] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Array</p> <p>True &nbsp; &nbsp; True &nbsp; &nbsp; Int32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.ValueType</p> <p>True &nbsp; &nbsp; True &nbsp; &nbsp; Int32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.ValueType</p> <p>True &nbsp; &nbsp; True &nbsp; &nbsp; Int32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.ValueType</p> <p>True &nbsp; &nbsp; True &nbsp; &nbsp; Int32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.ValueType</p> <p>True &nbsp; &nbsp; True &nbsp; &nbsp; Int32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.ValueType</p> <p>True &nbsp; &nbsp; True &nbsp; &nbsp; Int32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.ValueType</p> <p>True &nbsp; &nbsp; True &nbsp; &nbsp; Int32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.ValueType</p>

  • <p>@jrv,</p> <p>I think the [array] cast is unnecessary. What&#39;s happening because of operator precedence is the first item (1) is being cast as it&#39;s own array giving you $a which is any object[] containing an array plus seven integers.</p> <p>$a = [array](1,2,5,6,3,2,9,1)</p> <p>or</p> <p>$a = 1,2,5,6,3,2,9,1</p> <p>probably gives you what you expect -- an array of 8 integers.</p>

  • <p>@Greg</p> <p>Exactly my point.</p> <p>I am not a big fan of use of casts in PowerShell. &nbsp;They can have some difficult to find side-effects. &nbsp;We should not use constructs without a complete understanding of how they work in each situation. &nbsp;Scripting is intended to be sparse. &nbsp;Support for more fine-detailed code usage is good but only when needed.</p>

  • <p>Nice! Much faster than { $a | sort -Descending | select -First 1 } which is what I was doing so far. </p> <p>PS&gt; (measure-command { $a | sort -Descending | select -First 1 }).TotalMilliseconds</p> <p>3.879</p> <p>PS&gt; (measure-command { $a | measure -Maximum }).TotalMilliseconds</p> <p>1.3444</p>