Copy files and folders created yesterday

Hey All,

I(absolute beginner in powershell) have been trying to create powershell script powershell script to copy files and folders created a day before to another drive. The source is a some what complicated structure which has several folders and files under one main folder. The files and folders get created every day. Objective is to copy all the files and folders created a day before under the main folder to the mainbackup folder. I would like to run this script everyday to keep the main and the mainbackup folder in sync. So far I have come until here:

$source = “\main"
Get-ChildItem -Path $source |
Where-Object {$_.creationtime -f (Get-date).Addhours(-24)} |
Copy-Item -destination $dest -Container -Recurse

This script works but it takes for ever and goes through every file and folder in the source and throws “already exists” message. Is there a way to only to look only for files created a day before or 24 hours before, and copy them to destination? Any advice is appreciated.

I have not tested this too much but I believe you want to use ‘-le’ instead of ‘-f’. I have never seen/used ‘-f’ before but you may just know something I do not.

Get-ChildItem -Path $source | Where-Object {$_.creationtime -le (Get-date).Addhours(-24)}

That -f is a format attribute not a filter.


Why don’t you use Robocopy instead with the /a

/A : Copy only files with the Archive attribute set.

you can incorporate that into your ps script

Hi there,

Thanks for the input. I am a newbie in powershell…good to know -f has been doing nothing lol :).

Hi Alex,

Thanks for the input. I started the script with robocopy but gave up and moved on to copy-item. Haven’t used the /a and going to give it a try. It seems that robocopy just doesn’t handle so many files and folders. It appears that it just copies the newly created folders and not the files in those folders.

Hi Michael,

Thanks for the input. I have tried the -le and -ge. But they seem to do the same thing. The script still goes through all the folders and not just the one created in the past 24 hours and gives “already exists” message.

Here’s a robocopy backup script I have. I had to throttle the speed for some reason:

robocopy "folder" \\server\folder /ipg:10 /fft /e /log:backup.log /tee /np /XD "\OFFICE14" "\PASWStatistics" /xf *.msu /ndl /purge

Hey Copy,

Check out Robocopy "Robust File Copy" - Windows CMD - for all the switches

Thanks Js.

Can you give a short breakdown of the what each switch does please?