'How to keep 2 folders in sync using powershell script
We have two folders:
FolderA: D:\Powershell\OriginalFolderB: D:\Powershell\copy
Now, I want to keep FolderA and FolderB in sync (i.e. when a user changes/adds/removes a file/directory in FolderA then same changes should happen in FolderB).
I tried :
$Date = Get-Date
$Date2Str = $Date.ToString("yyyMMdd")
$Files = gci "D:\Powershell\Original"
ForEach ($File in $Files){
$FileDate = $File.LastWriteTime
$CTDate2Str = $FileDate.ToString("yyyyMMdd")
if ($CTDate2Str -eq $Date2Str) {
copy-item "D:\Powershell\Original" "D:\Powershell\copy" -recurse
-ErrorVariable capturedErrors -ErrorAction SilentlyContinue;
}
}
But this would require a similar powershell script for deletion of files in FolderA and changes in FolderB.
Solution 1:[1]
Have you looked at Robocopy (Robust File Copy)? It can be used with PS and provides what your looking for i.e. it is designed for reliable copying or mirroring of folders (changes/adds/removes) just select the options as required.
Robocopy sourceFolder destinationFolder /MIR /FFT /Z /XA:H /W:5
The /MIR option mirrors the source directory and the destination directory. It will delete files at the destination if they were deleted at the source.
Solution 2:[2]
I think you should try the following, it works for me change the syncMode base on your requirement. 1 is for one-way sync source to target, 2 is dual-way sync
$source="The source folder"
$target="The target folder"
touch $source'initial.initial'
touch $target'initial.initial'
$sourceFiles=Get-ChildItem -Path $source -Recurse
$targetFiles=Get-ChildItem -Path $target -Recurse
$syncMode=2
try{
$diff=Compare-Object -ReferenceObject $sourceFiles -DifferenceObject $targetFiles
foreach($f in $diff) {
if($f.SideIndicator -eq "<=") {
$fullSourceObject=$f.InputObject.FullName
$fullTargetObject=$f.InputObject.FullName.Replace($source, $target)
Write-Host "Attemp to copy the following: " $fullSourceObject
Copy-Item -Path $fullSourceObject -Destination $fullTargetObject
}
if($f.SideIndicator -eq "=>" -and $syncMode -eq 2) {
$fullSourceObject=$f.InputObject.FullName
$fullTargetObject=$f.InputObject.FullName.Replace($target,$source)
Write-Host "Attemp to copy the following: " $fullSourceObject
Copy-Item -Path $fullSourceObject -Destination $fullTargetObject
}
}
}
catch {
Write-Error -Message "something bad happened!" -ErrorAction Stop
}
rm $source'initial.initial'
rm $target'initial.initial'
Solution 3:[3]
in addition to previous answers this article about the way you compare files might also help. to actually compare file by content needs an additional step. (like hashing). a detailed description of this method is written here: https://mcpmag.com/articles/2016/04/14/contents-of-two-folders-with-powershell.aspx
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Paul Fijma |
