Copy-Item copies directory as well as contents to UNC path

PowershellUncPowershell 1.0

Powershell Problem Overview


I'm trying to take the contents of a folder and copy it to another using PowerShell 1.0. Pretty simple stuff and it all works fine using Copy-Item $from $to -recurse if I am copying from a local folder to a local folder. However, if the $to variable is a UNC path, it seems to copy the $from directory, not just its contents.

e.g.

$from = "c:\temp\rhysc\" 
$to = "\\OtherMachineName\ShareFolder\"  
Copy-Item $from $to -recurse

...ends up up creating a folder \\OtherMachineName\ShareFolder\rhysc instead of just copying over the contents of the folder.

I want to maintain the structure of the $from directory that I am copying over so my basic attempt at piping didn't work (everything got dumped in the root of the $to folder)

Get-ChildItem $from -recurse | Copy-Item -destination $to

Powershell Solutions


Solution 1 - Powershell

Try:

$from = "c:\temp\rhysc\*"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRhysCView Question on Stackoverflow
Solution 1 - PowershellDavid TchepakView Answer on Stackoverflow