Try adding a trailing back slash in the destination.
Copy-Item c:\MyTestFolder\* -Destination \\$comp1\c$\Test\ -Force -Recurse
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
Try adding a trailing back slash in the destination.
Copy-Item c:\MyTestFolder\* -Destination \\$comp1\c$\Test\ -Force -Recurse
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.
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.