How Do I copy the files and the Folder Tree to Remote Machine?
14,920
Solution 1
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.
Solution 2
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.
Comments
-
Selwyn almost 2 years
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
-
Selwyn over 14 yearsi tried it but it copies all the folders as well as copies all the files from the folder to the root of the directory
-
Zorayr over 10 yearsThis statement copies folders and files in a flat structure.