Author Posts

June 24, 2015 at 11:07 am

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 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

June 24, 2015 at 11:10 am

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.

June 24, 2015 at 11:17 am

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 ??

June 24, 2015 at 11:20 am

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

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

June 24, 2015 at 11:23 am

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
 }

June 24, 2015 at 12:01 pm

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

}

June 24, 2015 at 12:31 pm

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.

June 24, 2015 at 12:31 pm

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

June 25, 2015 at 5:16 am

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.