15424

Powershell: return filename with highest number

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

Answer1:

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

Recommend