Copying with powershell recursively

YaroC 316 Reputation points
2020-12-17T16:18:00.91+00:00

I'm trying to copy an entire folder with to a remote location and finding some unexpected behavior I was hoping someone can explain. So I have like a local path c:\MyTestFolder. This contains a couple of files and a subfolder with a single file. Trying to copy it to a remote path with a new name like so

Copy-Item c:\MyTestFolder\* -Destination \\$comp1\c$\Test -Force -Recurse

When adding -verbose I can see the first action being performed is
"Copy-Directory" from my source to \MyComp1\c$\Test\subfolder1 followed by
"Create Directory" \Mycomp1\c$\Test and then the file that's within the subfolder1 ends up being copied to wrong location
"Copy file" c:\MyTestFolder\subfolder1\sub_file1.txt to \$comp1\c$\Test\sub_file1.txt

So it looks like the file can't be copied to right location since the Test folder is getting created in the second step rather than in first which messes up the process. The subfolder1 folder never gets created unless I run the same line again in which case it get's created in second step and then the sub_file.txt get's copied to it as expected. Other than specifically creating an empty folder before starting to copy is there anything else that can be corrected in my one-liner to avoid this to happen?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2020-12-17T19:07:15.307+00:00

    Try adding a trailing back slash in the destination.

    Copy-Item c:\MyTestFolder\* -Destination \\$comp1\c$\Test\ -Force -Recurse
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Oner Ziya Bas 81 Reputation points
    2020-12-17T17:31:49.71+00:00
    0 comments No comments

  2. Anonymous
    2020-12-18T07:46:15.653+00:00

    Hi,

    Try removing * in the source path

     Copy-Item c:\MyTestFolder -Destination \\$comp1\c$\Test -Force -Recurse  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. YaroC 316 Reputation points
    2020-12-21T15:16:42.797+00:00

    All 3 proposed answers worked although MotoX80 showed exactly what I needed as this method worked even when my Test folder was already present at the destination while other method forced creating MyTestFolder within the existing Test folder doubling all the contents which is what I wanted to avoid. True, this can be dug out from the docs as well but isn't that clear in there what happens if parts of the path are already present at destination.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.