0

I don't understand this error message I'm receiving when I try to run my powershell script. The purpose is to copy a .bat file into the main win 7 startup folder on a series of machines.

enter image description here

And the script I am running.

$ServerList = Get-Content "C:\ServersList.txt" #Change this to location of servers list
$SourceFileLocation = "C:\firefox_issue.bat" #For example: D:\FoldertoCopy\ or D:\file.txt
$Destination = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup" #Example: C$\temp

foreach ($_ in $ServerList)
 {Copy-Item $SourceFileLocation -Destination \\$_\$Destination -Recurse -PassThru}


Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Write-Host
Write-Host "A"
Write-Host "B"
Write-Host "C"

2 Answers 2

1

Because your location is getting set to:

\\SERVERNAME\C:\ProgramData...

and it should be:

\\SERVERNAME\C$\ProgamData...

Your destination needs to be:

$Destination = 'C$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup'

And your loop should be:

foreach($server in $serverList) {
  Copy-Item $SourceFileLocation -Destination "\\$server\$Destination" -Recurse
}

You should probably avoid explicitly using $_ as a variable name as $_ is a special variable for accessing an object in the pipeline.

Sign up to request clarification or add additional context in comments.

Comments

1

Did you read the comment behind the $Destination line? This is a UNC path. \\server1\c:\programdata\ is not a valid UNC-path. Try:

$Destination = "C$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"

Also, $_ is a reserved variable for pipeline input, so you need to change it, like:

foreach ($server in $ServerList)
 {Copy-Item $SourceFileLocation -Destination \\$server\$Destination -Recurse -PassThru}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.