powershell copy only if different

i have a computer i want to move files from the share to only if the files on the share are different than those on the computer.

for example
$folder1 = c:\localfolder  has a file in it test.txt with a modify date of 10/22

$folder2 =  \\server\serverfolder has a file in it test.txt with a modify date of 10/23

i want compare the $folder1 and $folder2 and if $folder2 item(s) are newer than $folder1 then copy them to $folder1

i'm confused on how to do this
please help thanks!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I would not reinvent the wheel, and just run RoboCopy, which comes with the OS.
I'm not clear about the exact condition:
only files already existing locally?
move or copy?
with subfolders?
bbimisAuthor Commented:
ok then robocopy is fine just not sure how to use it (i will have to google it or maybe you can shed some light)
either way i want to copy not move only the newest files basically update the directory for any new content
and also subfolders

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Perfect for RoboCopy:
robocopy  \\server\serverfolder c:\localfolder /s

Open in new window

is the simplest way. It will copy all changed or new files and folders, without taking over security info. It will also not remove files from the target, so you do not have an exact copy - if files are deleted on the share, they stay in the local copy.

RoboCopy can do a lot more, like trying to make an exact mirror (with /MIR), retry on error, inserting some delay between files, ...
Run robocopy /? to get all the options.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

bbimisAuthor Commented:
I have a question
for some reason i keep getting this error when trying to copy over the my documents folder
i have made the backup on one computer in a folder called documents
i run the command
 robocopy z:\documents c:\users\manager\documents /e /is 

Open in new window

i keep getting this error?

aiting 30 seconds... Retrying...
2015/10/27 07:01:42 ERROR 3 (0x00000003) Scanning Destination Directory c:\users\manager\documents\My Music\
The system cannot find the path specified.
basically that file when i run it manual gives access denied is there a way to skip that or do i simply need to purge the my music directory from the main backup prior to running the script ?

one other question, is there a way to log everything to a text file?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
For logging, use /LOG:Logfile.txt (overwrite) or /LOG+:Logfile.txt (append). You might want to switch off header and footer info, directory info, etc. - see the switches starting with /N in robocopy /?.

Regarding those folders, you can exclude them manually: /XD "My Music","My Pictures" (aso), but that can get tedious; or try if /XJ ignores them; or ignore errors after 2 tries with /R:2 /W:5 (2 tries within 5 seconds). I recommend the latter, maybe with changed retries / wait time.
bbimisAuthor Commented:
thank you so much!!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.