Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 452
RhysC Avatar asked Dec 15 '09 06:12

RhysC


People also ask

How do I copy a folder and content in PowerShell?

Use Copy-Item Cmdlet to Copy Folder With Subfolders in PowerShell. The Copy-Item cmdlet copies an item from one location to another. You can use this cmdlet to copy the folder and its contents to the specified location. You will need to provide the source and destination path to copy from one place to another.

Does PowerShell copy item use SMB?

One cool feature that came with PowerShell v5 is this cmdlet's ability to not use the default SMB protocol for transferring a file but instead use WinRM and a PowerShell remote session. By using the Session parameter, Copy-Item uses an existing PowerShell session and transfers the files that way.

Does xcopy work in PowerShell?

xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command.


1 Answers

Try:

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

like image 136
David Tchepak Avatar answered Sep 28 '22 07:09

David Tchepak