Copy a file to remote computers


I am trying to copy a file to 180 computers in the network. I am using admin powershell prompt to execute the following code.

$computers = gc "C:\scripts\OBSVDIs.txt"
$source = "C:\Scripts\<filename>.csv"
$destination = "C$\<input path>"
foreach ($computer in $computers) {Copy-Item $source -Destination "\\$computer\$destination" -Recurse}

Copy-Item : The network path was not found
At line:1 char:36
+ ... computers) {Copy-Item $source -Destination "\\$computer\$destination" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

When I tried with single computer name in the C:\scripts\OBSVDIs.txt file, it worked. Not sure what is making difference when I use all 180 computers list in the file.

Any suggestion is greatly appreciated.


Make sure there are no leading or trailing spaces in the text file, its always good to trim $computer.trim() when reading computer names from a txt file.