Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Synchronize 2 folder paths using PowerShell

I would like to compare two folders, and copy any files that are missing.

I would like to compare this path:   C:\Users\Users1\Folder1

with this path:  C:\Users\Users1\Folder2

I would like to synchronize both folders so that if there is a difference then any files in the missing path will be copied from the path that has the files. 


How can I accomplish this using PowerShell?

Avatar of oBdA
oBdA

You can do that with robocopy.exe.
Very basic command; this is in test mode and will not actually copy anything; remove the /L ("log only") at the end to run it for real:
& robocopy.exe "C:\Users\Users1\Folder1" "C:\Users\Users1\Folder2" /r:0 /L

Open in new window

To sync back, just swap source and target:
& robocopy.exe "C:\Users\Users1\Folder2" "C:\Users\Users1\Folder1" /r:0 /L

Open in new window

Note that robocopy will by default copy files that differ (date or size) and skip over files that are already in the target.
If you want to sync back, you'll probably want to exclude newer files from being overwritten, so you might want to add /xo to the options.
To process subdirectories, add /e

If you rather want to remove files from the target that aren't in the source anymore (obviously not compatible with syncing back), add the /mirror argument.

For complete help, use
robocopy.exe /?
Avatar of E=mc2

ASKER

Thanks oBdA, so there is not a command to just synchronize both folders together.. ?
But it needs to be handled one way and then the other way back?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I use Freefilesync.  It syncs both ways and can sync multiple folder trees in the same job.  It works from a GUI and from a batch file.  You create a job in the GUI and you can run it and/or you can save to a batch file.  The really nice thing, is the GUI will show what files its going act on before you actually commit the sync.  You can then adjust the options and see how it affects the file actions.

Its got plenty of comparison and sync options to cover just about any specific you need.  for example:
  1. Adjust to ignore time zone or daylight savings differences.
  2. Robust filtering capability
  3. Sync method (2way, mirror, update, custom)
  4. Manage deleted files (RecyleBin, Permanent, User folder)
  5. Compare Time&Size, Size or content

See:  https://freefilesync.org/