-
Notifications
You must be signed in to change notification settings - Fork 6
/
SyncFileSystem.ps1
29 lines (21 loc) · 1009 Bytes
/
SyncFileSystem.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$SourceFolder = "D:\Source"
$TargetFolder = "D:\Target\"
$SourceFolderItems = Get-ChildItem -Recurse -Path D:\Source
$TargetFolderItems = Get-ChildItem -Recurse -Path D:\Target
try {
$Result = Compare-Object -ReferenceObject $SourceFolderItems -DifferenceObject $TargetFolderItems
# Symbol == it means that it exists in both sides
# Symbol <= it means that it exists in the SourceFolder but not in the TargetFolder
# Symbol => it means that it exists in the Target but not in the Source
foreach ($folder in $Result) {
if ($folder.SideIndicator -eq "<=") {
$FullSourceObject = $folder.InputObject.FullName
$FullTargetObject = $folder.InputObject.FullName.Replace($SourceFolder, $TargetFolder)
Write-Host "Attempt to copy the following: " $FullSourceObject
Copy-Item -Path $FullSourceObject -Destination $FullTargetObject
}
}
}
catch {
Write-Error -Message "Something bad happened!" -ErrorAction Stop
}