2

Suppose I have a list of filenames like this:

Get-ChildItem "Antarctica Data *.xls"

Antarctica Data 03625516.xls
Antarctica Data 84327262.xls
Antarctica Data 16374175.xls
Antarctica Data 12804427.xls
Antarctica Data 17477809.xls
Antarctica Data 60943758.xls
Antarctica Data 93962083.xls
Antarctica Data 74412097.xls
Antarctica Data 81562648.xls
Antarctica Data 58371936.xls

How can I use Powershell to return a string of the filename with highest number?

Antarctica Data 93962083.xls
Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
vxs8122
  • 834
  • 2
  • 10
  • 22

1 Answers1

4

Because the names only differ by the numbers, all you need to do is sort them and then get the last one:

Get-ChildItem "Antarctica Data *.xls" |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name

Or, more concisely:

(gci "Antarctica Data *.xls" | sort)[-1].Name

Output:

Antarctica Data 93962083.xls

  • +1 Be aware that this answer only works if all the numbers are 8 digits long. Ex. 99 will be sorted as higher then 98765432. Judging by the sample, it should be alright. Just wanted to mention it for future readers. :) – Frode F. Jul 23 '14 at 18:26