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

1

15

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?

Geminian answered 5/12, 2014 at 21:10 Comment(4)
Totally depends on what you want to do. You missed Robocopy in your list.Chelton
This is too broad as it stands. We dont know what you are moving, how much. There are many differences and you would almost always want to use xcopy/robocopy over the basic copy-item for batch jobs.Parasympathetic
While the title is ambiguous the post is fairly obviously asking if he should convert the xcopy <source> <dest> <options> in his existing batch file to the PS native Copy-Item cmdlet, or just leave it as is. We do need more info, but I feel the comments and down votes are being overly harsh on this one.Wound
The questions is edited. "Totally depends on what you want to do" is fair given how my question was phrased. But the point of the question was to find out what it depends on.Geminian
W
9

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.

What answered 5/12, 2014 at 21:54 Comment(2)
The solution that I ended up going with was to read the list of files to copy from a text file and pipe them to Copy-Item. This methodology works very consistently, even when piping them to my custom functions for anything such as registering DLLs.Geminian
The bug with Remove-item is still not fixed, even in Posh v5. MS site says: "When it is used with the Include parameter, the Recurse parameter might not delete all child directories or all child items. This is a known issue; as a workaround, try piping results of the Get-ChildItem -Recurse cmdlet into the Remove-Item cmdlet, as described in Example 4 in this topic". technet.microsoft.com/en-us/library/hh849765.aspxCoble

© 2022 - 2024 — McMap. All rights reserved.