← script library

Remote Directory Synchronization with PowerShell

Files & FoldersJanuary 12, 2025

This PowerShell script efficiently synchronizes files from a local directory to a remote server or network share. It ensures that only new or updated files are copied, minimizing bandwidth and time.

powershell
$localDir = "C:\Path\To\Local\Directory"
$remoteDir = "\\Server\Share\Path"

if (-not (Test-Path $remoteDir)) {
    Write-Host "Error: Remote directory is not accessible. Please check the path and permissions." -ForegroundColor Red
    exit
}

$localFiles = Get-ChildItem -Path $localDir -File
$remoteFiles = Get-ChildItem -Path $remoteDir -File
$filesToSync = $localFiles | Where-Object {
    -not ($remoteFiles | Where-Object { $_.Name -eq $_.Name -and $_.LastWriteTime -eq $_.LastWriteTime })
}

foreach ($file in $filesToSync) {
    try {
        Copy-Item -Path $file.FullName -Destination $remoteDir -ErrorAction Stop
        Write-Host "Successfully synchronized $($file.Name) to $remoteDir"
    } catch {
        Write-Host "Failed to synchronize $($file.Name): $_" -ForegroundColor Red
    }
}

$syncedCount = $filesToSync.Count
Write-Host "$syncedCount file(s) have been synchronized to $remoteDir."

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.