Below is a simple PowerShell script example that copies a folder from one server to another using a UNC (Universal Naming Convention) path. This script uses the Copy-Item
cmdlet to copy the folder and its contents. Ensure you have the necessary permissions to access both the source and destination paths.
Before you run the script, replace \\SourceServer\SharedFolder
with the UNC path of the source folder you want to copy and \\DestinationServer\DestinationFolder
with the UNC path of the destination where you want the folder to be copied.
powershellCopy code# PowerShell script to copy a folder from one server to another using UNC paths
# Define the source and destination UNC paths
$sourcePath = "\\SourceServer\SharedFolder"
$destinationPath = "\\DestinationServer\DestinationFolder"
# Check if the source folder exists
if (Test-Path -Path $sourcePath) {
# Copy the folder from source to destination, including all subfolders and files
Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse -Force
Write-Host "Folder copied successfully from $sourcePath to $destinationPath."
} else {
Write-Host "Source folder does not exist: $sourcePath"
}
This script copies all contents from the source folder to the destination, including subfolders and files. The -Recurse
parameter is used to ensure that all subdirectories and files are copied, and -Force
is used to overwrite files in the destination if they already exist.
Important considerations:
- Permissions: Make sure the account running the script has read access to the source path and write access to the destination path.
- Network Latency and Bandwidth: Copying large folders over a network can be time-consuming and may impact network performance. Consider running such operations during off-peak hours.
- Error Handling: The above script is basic and does not include detailed error handling. You may want to add try-catch blocks or additional checks to handle potential errors more gracefully.
Always test scripts in a safe environment before using them in production to ensure they work as expected and do not cause unintended effects.