60

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
Cy Rossignol
  • 16,216
  • 4
  • 57
  • 83
RhysC
  • 1,644
  • 1
  • 15
  • 23
  • Are you sure the local copy didn't also copy the directory? That's the behavior I see unless you do what David suggests. – Keith Hill Dec 15 '09 at 15:52
  • yep, 100% sure, my tests were relying on it. It only broke changed behaviour when i changed the path to UNC. – RhysC Dec 16 '09 at 03:11
  • See also [How can I copy a directory, overwriting its contents if it exists using Powershell?](http://superuser.com/q/544520) – Michael Freidgeim Feb 04 '16 at 22:57

1 Answers1

76

Try:

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

David Tchepak
  • 9,826
  • 2
  • 56
  • 68