We help IT Professionals succeed at work.
Get Started

copy files to remote computers using powershell

karkou12 asked
Last Modified: 2018-05-29
Hi Experts,

I am trying to copy a file to multiple servers using powershell.

$FullPath = $MyInvocation.MyCommand.Path
$Scriptpath = Split-Path -Path $FullPath -Parent
cd $Scriptpath

$servers= '.\servers.txt'
$Source = '.\TraceConsolidator.exe.config'
$outfile = ".\ViewResult.log"
$Destination = 'Program Files\ndwill mynde\Sym\SyM_configMgmt'

#$computers = Get-Content $servers

[string[]]$Computers = Get-Content "$servers"

If(Test-Path $outfile) {Remove-Item -Path $outfile -Force}

ForEach($computer in $Computers){
"Trying to  connect to $computer"
"Trying to  connect to $computer " >> $outfile

if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))

"Problem in connecting to $computer"
"Problem in connecting to $computer " >> $outfile
"***********************************">> $outfile

"Connected to $computer"
"Connected to $computer " >> $outfile

Copy-Item $Source -Destination '\\$computer\C$\Program Files\ndwill mynde\Sym\SyM_configMgmt' -Force
if (Test-Path "\\$computer\C$\Program Files\ndwill mynde\Sym\SyM_configMgmt\TraceConsolidator.exe.config") 
write-host "copying the file to the C:\$Destination in $computer is successful"
"copied the file to the C:\$Destination in $computer " >> $outfile
"**************************************************">> $outfile


write-host "copying the file to the C:\$Destination in $computer is not successful"
  "copying the file to the C:\$Destination in $computer is not successful " >> $outfile
  "***********************************">> $outfile


Open in new window

But the script says it is connected to the computer and tbe says as below

Copy-Item : Could not find a part of the path '\\\C$\Program Files\ndwill mynde\Sym\SyM_configMgmt.  This works fine if there is no space in the destination  path.

Can you help me with what I am missing here.
Watch Question
Transport Ninja
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE