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

RhysC picture RhysC · Dec 15, 2009 · Viewed 80.1k times · Source

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

Answer

David Tchepak picture David Tchepak · Dec 15, 2009

Try:

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