Powershell get changed files and copy them

filesystemspowershell

Trying to copy all changed files from a directory (recursively) to a new location, keeping the directory structure intact.

I put the changed files into an array:
$datechanged = ($datechanged).addyears(-3)
$changedfiles = ls -recurse | where-object{$_.lastwritetime -gt $datechanged}

Now I would like to copy these files (and directories?) to a new location, but am not sure how to proceed.

I have been trying the syntax of foreach, but I am not having much luck. I am doing this as a learning exercise, but with an actual goal in mind of archiving these changed files – so putting the files directly into a 7-zip file would be great for speed, but I would like to better understand how to deal with these arrays of objects and pipeline them.

Thanks!

Best Answer

You are reinventing the wheel here. Robocopy will do exactly what you want.

Edit: on the off chance that you are already aware of robocopy, I found powershell.com's Mastering PowerShell ebook quite a good starting point for getting to grips with PowerShell.