Powershell, Sort a list of files by version number at the end of the filename?

Question:

How would you sort this filename list with PowerShell so they will appear in descending version order?

I only need the highest version filename.

The following will select “CYFS_PreK_1_0_1_9” since it is the highest number alphabetically as there are no leading zeros in the version numbers.

However, I am looking for “CYFS_PreK_1_0_1_23”

UPDATE:

If we only care about the final set of numbers, we can split the name for underscores and sort the final segment numerically.

This works for this set, however, if we rev to version 1_0_2_x, then it breaks again as the final 1 in 1_0_2_1 is less than 23 in 1_0_1_23.

Answer:

You can use the [Version] type to do the version sorting. This only takes the version into account (so it doesn’t care about the beginning of the filename):

Source:

Powershell, Sort a list of files by version number at the end of the filename? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply