6

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
Zorayr
  • 23,770
  • 8
  • 136
  • 129
Selwyn
  • 1,621
  • 6
  • 21
  • 38

4 Answers4

5

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.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Zorayr
  • 23,770
  • 8
  • 136
  • 129
1
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.

Robert Groves
  • 7,574
  • 6
  • 38
  • 50
-3

I would assume you have rights for the 2 servers

Get-ChildItem "\SERVER_A\C$" -Recurse | Copy-Item -Destination" \SERVER_B\C$"

acermate433s
  • 2,514
  • 4
  • 24
  • 32
-3

Don't use PowerShell's Copy-Item cmdlet. Find a third-party mirroring/synchronization tool. We use RoboCopy.

Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91