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
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.
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.
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