Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell use xcopy, robocopy or copy-item [closed]

The reason for switching from batch files to powershell scripts is to improve error checking of the process. Does the cmdlet for copying have advantages in this regard?

If a batch file already exists that uses xcopy to copy files by filename individually is there any advantage to converting the syntax to copy-item?

What are the advantages of using robocopy, xcopy and copy-item (compared to each other)? For example does robocopy have an advantage when working with a large number of small files over a reliable network. If this script is to be run simultaneously on hundreds of computers to copy hundreds of files to each of them will that affect the decision? Should the decision be focused mainly on the permissions of the files?

like image 255
Adam Avatar asked Dec 05 '14 21:12

Adam


People also ask

Should I use xcopy or Robocopy?

Xcopy vs Robocopy: MirroringRobocopy is used to mirror or sync directories while Xcopy does nothing about that. Robocopy can check the destination directory and delete all the files no longer in the main tree rather than copy all the files from one directory to another.

Does xcopy work in PowerShell?

xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command.

How do I copy only the latest or updated files in PowerShell?

To copy only updated or newer files with PowerShell, we can use Copy-Item with some logic in the script that to check if the files exist on the destination folder if not then copy that file and if yes then compare the timestamp and copy the latest file.


1 Answers

The primary advantage is just that you can send objects to Copy-Item through a pipe instead of strings or filespecs. So you could do:

Get-ChildItem '\\fileserver\photos\*.jpeg' -File | `
  Where-Object { ($_.LastAccessTime -ge (Get-Date).AddDays(-1)) -and ($_.Length -le 500000) } | `
  Copy-Item -Destination '\\webserver\photos\'

That's kind of a poor example (you could do that with Copy-Item -Filter), but it's an easy one to come up with on-the-fly. It's pretty common when working with files to end up with a pipeline from Get-ChildItem, and I personally tend to do that a lot just because of the -Recurse -Include bug with Remove-Item.

You also get PowerShell's error trapping, special parameters like -Passthru, -WhatIf, -UseTransaction, and all the common parameters as well. Copy-Item -Recurse can replicate some of xcopy's tree copying abilities, but it's pretty bare-bones.

Now, if you need to maintain ACLs, ownership, auditing, and the like, then xcopy or robocopy are probably going to be much easier because that functionality is built in. I'm not sure how Copy-Item handles copying encrypted files to non-encrypted locations (xcopy has some ability to do this), and I don't believe Copy-Item supports managing the archive attribute directly.

If it's speed you're looking for, then I would suspect that xcopy and robocopy would win out. Managed code has higher overhead in general. Xcopy and robocopy also offer a lot more control over how well they work with the network.

like image 110
Bacon Bits Avatar answered Sep 29 '22 11:09

Bacon Bits