Hi Lev
! Here's an example script that copies a large amount of files from one Windows server to another using PowerShell:
$sourcePath = "C:\Path\to\source\folder"
$destinationPath = "\\ServerName\C$\Path\to\destination\folder"
# Create a new instance of the Robocopy object
$robocopy = New-Object -TypeName System.Diagnostics.Process
# Set the Robocopy parameters
$robocopy.StartInfo.FileName = "robocopy.exe"
$robocopy.StartInfo.Arguments = "`"$sourcePath`" `"$destinationPath`" /E /ZB /MT:8 /R:3 /W:5 /NP /LOG+:C:\Path\to\log\file.log"
# Start the Robocopy process
$robocopy.Start() | Out-Null
# Wait for the Robocopy process to complete
$robocopy.WaitForExit()
# Check the exit code of the Robocopy process
if ($robocopy.ExitCode -eq 0) {
Write-Host "File copy completed successfully."
} else {
Write-Host "File copy failed. Exit code: $($robocopy.ExitCode)"
}
In this script, we use the robocopy
command-line tool, which is built into Windows, to perform the file copy operation. The script sets various parameters for Robocopy, such as /E
to include subdirectories, /ZB
to use restartable mode (in case of interruptions), /MT:8
to use 8 threads for multithreaded copying, /R:3
to retry failed copies up to 3 times, /W:5
to wait 5 seconds between retries, /NP
to exclude the progress percentage from the log, and /LOG+
to append the output to a log file.
You need to modify the $sourcePath
, $destinationPath
, and the /LOG+
argument to fit your specific paths and log file location. The Start()
method starts the Robocopy process, and WaitForExit()
waits for the process to complete. The script then checks the exit code of Robocopy to determine if the copy operation was successful.
Please make sure you have appropriate permissions and access rights on both the source and destination servers for the file copy to work correctly.