Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Copy most recent files from a list of directories

Copy most recent files from a list of directories

Copy most recent files from a list of directories

I want to give PowerShell an explicit list of file paths to several directories and have it copy the most recent file found in each directory to a common location.

e.g., given the list


I want to get the most recent file out of each of those directories and copy it to another directory named


I have scripts to copy the entire contents of a directory, or to copy an explicit file name from a directory, but not to get the most recent file regardless of its name.


Richard Ray
Jackson Hole Mountain Resort

RE: Copy most recent files from a list of directories

i don't know the syntax, but the logic would look like this:
for each directory
1. get a list of all files in the directory
2. order files by create date in descending order
3. take the first file
4. copy the file

in c# this is simple enough


new [] {
.Select(directory => new DirectoryInfo(directory))
.Where(directory => directory.Exists)
.Select(directory => directory
                      .OrderByDescending(file => file.CreationTime)
.Where(file => file != null)
.ForEach(file => File.CopyTo(@"C:\ForArchive\")
with PS i'm sure you could somehow pipe for a clean script. something like


select-object {get-childitem $_ | sort-object $_.CreationTime -desc | select-object -first 1} | where $_ != null | copy-object $_ "c:\ForArchive\"

Jason Meckley

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close