Reputation: 1858
I want to copy the latest/modified files from a folder to another in PowerShell.
I am able to use the code below to see the latest files based on the LastWriteTime but I don't know how to copy them to another folder.
$latest = Get-ChildItem $dir -Recurse | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-7)}
Upvotes: 1
Views: 104
Reputation: 4798
This code collects the required files into a $latest
variable. Afterwards, the files, specified in the variable can be moved as follows:
Copy-Item $latest -Destination D:\Tmp -Recurse
Another way is to use the command pipeline instead of collecting the files into the variable:
Get-ChildItem $dir -Recurse | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} | Copy-Item -Destination D:\Tmp -Recurse
Obviously, you can use any other directory or variable instead of D:\Tmp
Upvotes: 1