Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I copy the files and the Folder Tree to Remote Machine?

I have two machines Server A and Server B, and I want to copy all the files and folder tree from Server A to Server B using PowerShell.

I have tried the command given below, but it copies only the files in the folder and does not create the folder tree:

Copy-Item E:\TestSource\* //TestDestination/ -recurse -force
like image 210
Selwyn Avatar asked Aug 27 '09 06:08

Selwyn


2 Answers

If you have a source and a destination folder, you could use the following command:

robocopy $source $dest /e

If you need any more information about the command, you could use robocopy /?, or visit Robocopy documentation at Windows Server Technet, Robocopy.

like image 162
Zorayr Avatar answered Oct 10 '22 07:10

Zorayr


Get-ChildItem \\server_a\c$ -Recurse | % {
   $dest = Join-Path -Path \\server_b\c$ -ChildPath (Split-Path $_.FullName -NoQualifier)

   if (!(Test-Path $dest))
   {
      mkdir $dest
   }

   Copy-Item $_.FullName -Destination $dest -Force
}

Using Split-Path with the -NoQualifier parameter will return the source path without the drive information. Join that with your destination path prefix and use the result to create the destination directory (if it does not exist) and perform the copy.

like image 35
Robert Groves Avatar answered Oct 10 '22 06:10

Robert Groves