Sort files in folders by version

Hi All.
I am PowerShell beginner , kindly ask you to help me with this non-trivial task.
I have full backup of the SharePoint Libraries including all documents revisions and versions.
For example:

LoDs TM1_Project and Engineering.8.0.xlsx
LoDs TM1_Project and Engineering.9.0.xlsx
LoDs TM1_Project and Engineering.10.0.xlsx
LoDs TM1_Project and Engineering.11.0.xlsx

The version value may be a 1, 2, 3 or 4 digit
How to select and copy only the latest version of each file?
It would be LoDs TM1_Project and Engineering.11.0.xlsx from example.
Sorting by date not applicable.

K Rustam72,
Welcome to the forum. :wave:t4:

That’s actually a very non-trivial task. There are in fact more than enough examples out there.

What have you tried so far? We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Assuming the latest version is the one with last write access you should be able to sort for the property LastWriteTime and pick the first or the last depending on the sorting order. :wink:

Yes, you are right.
This is my script

Get-ChildItem -Path "\\path" -Recurse |
Sort-Object -Property LastWriteTime -Descending | 
Export-Csv -Path "E:\Apps\TM.csv" -Encoding UTF8

but is still have just list of files sorted by time, millions files :face_with_spiral_eyes:
So how can I select and copy to another folder only the last modified files?

Piping Select-Object -first 1

Get-ChildItem -Path "\\path" -Recurse |
Sort-Object -Property LastWriteTime | Select-Object -first 1 | Export-Csv -Path "E:\Apps\TM.csv" -Encoding UTF8

returns only one the latest file from whole directory and also csv report becames unreadable

You have to search for duplicates. But it is up to you to decide what’s a valid duplicate. If your files follow a naming convention it should be easy.

Assumed your files names all look similar to the ones you posted you could split the file names on the first period and use the first part of the file name to group the result. Then you can pick only the newest file from each group and you’re done. :wink:

You may read more about

You should always read the complete help topic including the examples to learn how to use it.

The problem is that all files doesn’t locates in one folder and doesn’t have similar filenames.
The root directory contains thousands child folders. My example is just short view from one folder.
TM\Construсtion\Documents\Documents\Project Control\LIST OF PROJECTS 2021-2023.1706.0.xlsx (1705 files stays before :upside_down_face:)
TM\Construсtion\Documents\Documents\Project Control\Weekly Status Report of Key Projects_May_week_3.6.0.xlsx
And so on

That shouldn’t be a problem …

That could be a problem … :wink:

At the end of the day you have to find a way to distinguish the different groups of versioned files. But it will be hard if they don’t have a common attribute you can rely on. You will have to find the unique or actually the unversal pattern what separates the actual file name from the version info. For the file names you poasted last it is the first period in the first file name and the last underline for the second file name.