Author Posts

November 3, 2017 at 9:53 pm

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\"
$dest="\\mainbackup"
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.

November 3, 2017 at 10:43 pm

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)}

November 4, 2017 at 10:42 pm

That -f is a format attribute not a filter.
'technet.microsoft.com/en-us/library/ee692801.aspx
'technet.microsoft.com/en-us/library/ee692795.aspx'

November 6, 2017 at 12:59 am

Hey,

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

November 6, 2017 at 4:15 pm

Hi there,

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

November 6, 2017 at 4:20 pm

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.

November 6, 2017 at 4:22 pm

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.

November 6, 2017 at 6:09 pm

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 gallery.zip *.msu gallery.zip /ndl /purge

November 6, 2017 at 9:09 pm

Hey Copy,

Check out https://ss64.com/nt/robocopy.html for all the switches

November 14, 2017 at 10:23 pm

Thanks Js.

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