Mimic Sync Center

Define Source and Destination Path

<p>$localPath = “C:\Users\Username\Documents” # Local folder<br>$networkPath = “\FileServer\Share\Documents” # Network folder</p>

<h1>Check if the network path is accessible</h1>

if (Test-Path $networkPath) {

# Check if the local folder exists; if not, create it

if (-not (Test-Path $localPath)) {

New-Item -ItemType Directory -Path $localPath | Out-Null

}

# Get the list of files in the local folder

$localFiles = Get-ChildItem -Path $localPath

# Loop through each local file and synchronize with the network folder

foreach ($file in $localFiles) {

$networkFilePath = Join-Path $networkPath $file.Name

$localFilePath = Join-Path $localPath $file.Name

# Compare file timestamps or other criteria to determine if synchronization is needed

if ((Get-Item $localFilePath).LastWriteTime -gt (Get-Item $networkFilePath).LastWriteTime) {

# Copy the local file to the network folder

Copy-Item -Path $localFilePath -Destination $networkFilePath -Force

} elseif ((Get-Item $localFilePath).LastWriteTime -lt (Get-Item $networkFilePath).LastWriteTime) {

# Copy the network file to the local folder

Copy-Item -Path $networkFilePath -Destination $localFilePath -Force
}
}

Write-Host “Synchronization completed.”
}
else
{
Write-Host “Network path not accessible. Check connectivity and try again.”
}

Leave a Reply