Fast PowerShell Script to Copy Latest SQL Backups Over Network

If you're managing SQL Server backups and need to automatically copy the latest .bak files from one server to another within the same domain and on a high-bandwidth connection, this PowerShell + robocopy script is optimized for speed and reliability.

function Copy-LatestBackupFast {
    param (
        [string]$SourceDir,
        [string]$DestDir
    )

    # Ensure destination exists
    if (!(Test-Path $DestDir)) {
        New-Item -ItemType Directory -Path $DestDir -Force | Out-Null
    }

    # Get the latest .bak file
    $Latest = Get-ChildItem -Path $SourceDir -Filter *.bak | Sort-Object LastWriteTime -Descending | Select-Object -First 1

    if ($Latest) {
        $SourcePath = $Latest.FullName
        $FileName   = $Latest.Name

        # robocopy works on directories, so we pass the directory and file
        $SourceFolder = Split-Path $SourcePath

        $cmd = @(
            "robocopy",
            "`"$SourceFolder`"",
            "`"$DestDir`"",
            "`"$FileName`"",
            "/COPY:D",              # Copy data only
            "/NFL", "/NDL",         # No file/dir listing
            "/NP",                  # No progress
            "/NJH", "/NJS",         # No job header/summary
            "/R:0", "/W:0",         # No retries/wait
            "/MT:32"                # Multi-threaded copy
        ) -join ' '

        Write-Host "Copying: $FileName from $SourceFolder to $DestDir ..."
        Invoke-Expression $cmd
    } else {
        Write-Warning "No .bak files found in $SourceDir"
    }
}

# === Copy Each Backup Set ===

Copy-LatestBackupFast "\\node1\adads"             "\\node\Backup\addad"
Copy-LatestBackupFast "\\node2\aff"                "\\node\Backup\addd"
Copy-LatestBackupFast "\\node3\addd"            "\\node\Backup\asdref"
Copy-LatestBackupFast "\\node3\addd"   "\\node\Backup\adeedd"
Copy-LatestBackupFast "\\node3\eeww"        "\\node\Backup\addd"