0

I have this foreach statement that goes through a list of usernames and puts each name in the path listed below, then it copies and pastes a file to the individual users startup folder. For some reason i get an error that a portion of the path was not found. Any ideas what the problem could be?

#for each username folder copy the a file to the users startup folder
foreach ($_ in $usernames)
{
$destination = "C:\users\"+ "$_" + "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
Copy-Item -Path c:\temp\file.bat -destination $destination -force
}

I have tried changing the way the path for my $destination variable is written and i get the same results

  • 1
    $_ is an automatic variable. You should probably not use it for your loop "counter". – Mike Shepard Jan 17 '18 at 16:00
  • Which Windows version/network? For instance, in a non-domain environment, the code snippet should run smoothly in an elevated PowerShell console. – JosefZ Jan 17 '18 at 17:29
  • @JosefZ windows 7 domain joined clients – veel84 Jan 17 '18 at 17:30
3

A couple of issues with this:

1) foreach ($_ in $usernames). Its really bad practise to try and inject into a pipeline variable and can be unpredictable at best. Instead try something like ForEach($username in $usernames)

2) your string build for $destination could do with better formatting. try something like $destination = "'C:\users\$username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'"

3) Indentation - makes it easier to read

Putting it all together - you get something like:

foreach ($username in $usernames)
{
    $destination = "'C:\users\$username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'"
    Copy-Item -Path c:\temp\file.bat -destination $destination -force
}

For extra points:

  • put the file you're copying into a variable for easier modifications or reuse

  • look to add an error handler and make sure the path exists before you try the copy

You then end up with something that looks like this:

$sourceFile = "C:\temp\file.bat"
foreach ($username in $usernames)
{
    $destination = "'C:\users\$username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'"
    if (Test-Path $destination) {
        try {
            Copy-Item -Path $sourceFile -destination $destination -force
            Write-Host "Copy Completed"
        } catch {
            Write-Host "Copy to $destination Failed"
        }

    } else {
        Write-Host "$destination Does Not Exist"
    }
}

You'll now be told if each copy succeeds, fails because the path doesnt exist or fails because the copy fails (access denied or similar)

| improve this answer | |
0

Well this is interesting. I got to looking at the error message which again read as follows:

Copy-Item : Could not find a part of the path 'c:\users\
                                                Public\AppData\Roaming\Micros oft\Windows\Start Menu\Programs\Startup'

So i realized that all the space between c:\users\ and Public was just white space. So i edited my list to remove all the white space after each username and after that it works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.