Copy 1 File to Multiple Computers

I’m having a devil of a time getting this script to work. I want to copy a local file (Public.lnk) to several other PC Desktops. I cannot get it to work and have tried several other variations. I’m a total newbe at PowerShell so any help would be appreciated .

PCList.txt is a list of IP addresses.

$PCList = Get-Content -Path C:\Scripts\PCList.txt

foreach ($PC in $PCList){
Copy-Item -Path C:\scripts\files\Public.lnk -Destination \$PC\C$\Users\Public\Desktop
}

This is the error I’m getting-
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
At C:\Scripts\CopyFile06.ps1:4 char:1

  • Copy-Item -Path C:\scripts\files\Public.lnk -Destination \$PC\C$\Users\Public\D …

I believe you may have to change your parameter. Try this:

$PCList = (Get-content -Path C:\Scripts\PCList.txt)

I’m fairly new at this so anybody else, feel free to correct me.

Thanks for the quick response - but that didn’t do it. I get the same error.
On all the examples I’ve studied (dozens), none put parenthesis around that parameter. It seems the issues is with the execution syntax ??

Have you tried to remove the Admin share ($) from your path?

-Destination \$PS\C:\Users\Public\Desktop

Hi Ken,
I believe the problem is in the Destination part.
Try escaping the $ in the destination path

# PCList.txt is a list of IP addresses.
 $PCList = Get-Content -Path C:\Scripts\PCList.txt

foreach ($PC in $PCList){
 Copy-Item -Path C:\scripts\files\Public.lnk -Destination \\$PC\C`$\Users\Public\Desktop
 }

When I Use this variation (below), It worked 1 time then after that, no errors show , and no file is copied either.

$PCList = Get-Content -Path C:\Scripts\PClist.txt
foreach ($PC in $PCList){
Copy-Item -Path C:\scripts\files\Public.lnk -Destination \Users\Public\Desktop\Public.lnk

}

That variation of the destination parameter doesn’t really go anywhere except perhaps your PC that you are executing the script from.

When I used the example I posted above in my test lab, it copied the file to 3 different pc’s.

The key element is the backtick between the C and the $. That escapes the $ so Powershell doesn’t think that’s the beginning of a variable name.

Ken,

Try something like this:

$pcnames = Get-Content -Path C:\Scripts\PClist.txt

foreach ($cn in $pcnames){

Copy-Item -Path 'C:\scripts\files\Public.lnk' -Destination "\\$CN\c`$\Users\Public\Desktop\" 
}

It worked for me. Do not add the Public.lnk in the -Destination part

Oh you won’t believe it - After running the script with the “Write-Output” cmdlet, I noticed 5 output lines when there should have been only 2 (2 PCs in my PCList.txt) along with a bunch of errors - As you can see below “\\c$\Users\Public\Desktop” shows 3 times. Where is this coming from ?? - I deleted the PCList.txt file and created a new one and the problem went away. Apparently there was something wrong with that .txt file that was not visible OR it was RTFM.

PS C:> C:\Scripts\CopyFile09.ps1
\PC-Annex\C$\Users\Public\Desktop
\it-office-02\C$\Users\Public\Desktop
\\c$\Users\Public\Desktop
\\c$\Users\Public\Desktop
\\c$\Users\Public\Desktop

The Script is working correctly now; Thanks to everyone who contributed and got me going in the right direction.