Powershell Script to Copy Single file to Multiple Servers

 To copy a single file to multiple servers using PowerShell, you can utilize a loop to iterate through the list of server names and use the `Copy-Item` cmdlet to perform the file copy. Here's an example script:



# List of server names

$serverNames = "Server1", "Server2", "Server3"


# Path to the source file

$sourceFilePath = "C:\Path\to\source\file.txt"


# Destination directory on the servers

$destinationDirectory = "C:\Path\to\destination\"


# Loop through each server and copy the file

foreach ($server in $serverNames) {

    $destinationPath = "\\$server\$destinationDirectory"

    $destinationFile = Join-Path -Path $destinationPath -ChildPath (Split-Path $sourceFilePath -Leaf)

    

    # Copy the file to the destination

    Copy-Item -Path $sourceFilePath -Destination $destinationPath -Force

    

    # Optional: Display the copy status

    if (Test-Path -Path $destinationFile) {

        Write-Host "File copied successfully to $server"

    } else {

        Write-Host "Failed to copy file to $server"

    }

}


Make sure to update the `$serverNames`, `$sourceFilePath`, and `$destinationDirectory` variables according to your specific requirements. The script will iterate through each server in the list, copy the source file to the destination directory on each server, and display a status message indicating whether the copy was successful or not.

Note: The script assumes that you have proper file and directory permissions on the servers and that you have administrative access to perform the file copy.

Post a Comment

Previous Post Next Post