function Get-FileHashValue { param ( [string]$FilePath ) if (Test-Path $FilePath) { return (Get-FileHash -Path $FilePath -Algorithm SHA256).Hash } else { return $null } } $SourcePath = "\\23.158.40.80\Accounting" $DestinationPath = "Z:\" # Ensure the destination exists if (!(Test-Path -Path $DestinationPath)) { Write-Host "Destination path does not exist: $DestinationPath" exit } # Get all files in source directory $SourceFiles = Get-ChildItem -Path $SourcePath -Recurse -File foreach ($SourceFile in $SourceFiles) { # Define relative path $RelativePath = $SourceFile.FullName.Replace($SourcePath, "").TrimStart("\\") $DestFile = Join-Path -Path $DestinationPath -ChildPath $RelativePath # Create destination folder if it does not exist $DestFolder = Split-Path -Path $DestFile -Parent if (!(Test-Path -Path $DestFolder)) { New-Item -ItemType Directory -Path $DestFolder -Force | Out-Null } # Get checksum values $SourceHash = Get-FileHashValue -FilePath $SourceFile.FullName $DestHash = Get-FileHashValue -FilePath $DestFile # Display checksum values Write-Host "Checking file: $SourceFile.FullName" Write-Host "Source Checksum: $SourceHash" Write-Host "Destination Checksum: $DestHash" # Copy if hashes are different or file does not exist if ($SourceHash -ne $DestHash) { Write-Host "Copying: $SourceFile.FullName -> $DestFile" Copy-Item -Path $SourceFile.FullName -Destination $DestFile -Force } }