views:

269

answers:

2

I have two machines Server A and Server B I want to copy all the files and folder tree from Server A to Server B and create the same file and folder tree using powershell scripting

I have tried the command given below but it copies only the files in the folder but do not create the folder tree

Copy-Item E:\TestSource\* //TestDestination/ -recurse -force

Please reply soon, its urgent

Thanks in Advance

A: 

I would assume you have rights for the 2 servers

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

acermate433s
i tried it but it copies all the folders as well as copies all the files from the folder to the root of the directory
Selwyn
A: 
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