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
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.
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.
xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command.
Try:
$from = "c:\temp\rhysc\*"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With