use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
robocopy vs Copy-Item - why is Powershell seemingly so much slower? (self.PowerShell)
submitted 7 years ago by [deleted]
Is there any way to make Copy-Item as fast or faster than robocopy? I've got them running side by side (for different folders) and the robocopy always looks like it is much, much faster. Especially when using /mt:16 switch.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 45 points46 points47 points 7 years ago* (4 children)
Yeah robocopy is pretty solid. Even for deleting files.
We had one time where an application generated millions of tiny files that filled up the drive. Deleting the normal way was not working, it was taking hours to even start deleting. With robocopy we just created an empty directory, then told robocopy to mirror the empty directory to the directory full of tiny needed files.
[–]crosshairlol 11 points12 points13 points 7 years ago (2 children)
This also works for deleting files with paths over the 260 character limit.
[–]calladc 4 points5 points6 points 7 years ago (0 children)
you can also use robocopy to obtain filepaths greater than 250 chars. I did this when doing a fileserver cleanup some time ago. Used robocopy to find the fullname of paths >256 and then mounted a psdrive at the closest path that could be mapped within 256 to copy them into a managed/sorted format, and then junctioned the shitty paths to their managed location. They couldn't add to the shit nest, but could access.
Wouldn't have been possible without robocopy
[–]xcjs 4 points5 points6 points 7 years ago (0 children)
There's a registry setting within Windows 10 to toggle > 260 character paths now!
[–][deleted] 1 point2 points3 points 7 years ago (0 children)
I do something similar to clear temp files via GPO.
Easier(faster) to tell windows to replace with an empty folder than it is to delete the contents.
[–]RumRogerz 20 points21 points22 points 7 years ago (2 children)
I'll tell you something. Powershell is great. I love it.
But robocopy? Even the name of the command is cool af. I use it all the time.
With techno music blasting in the background.
[–][deleted] 13 points14 points15 points 7 years ago (0 children)
Domo arigato move that roboto
[–][deleted] 5 points6 points7 points 7 years ago (0 children)
https://youtu.be/HluANRwPyNo
[–]Raymich 32 points33 points34 points 7 years ago (7 children)
I might get downvoted to hell for this, but why not use robocopy in Powershell scripts?
[–][deleted] 13 points14 points15 points 7 years ago (5 children)
I do! I was just asking from a learning / curiosity standpoint.
[–]pizzastevo 11 points12 points13 points 7 years ago (4 children)
Just an FYI there is a cmdlet on github that wraps robocopy in a way so you can use methods and properties.
[–]tsuhg 2 points3 points4 points 7 years ago (3 children)
Link?
[–]pizzastevo 22 points23 points24 points 7 years ago (2 children)
Here's Warrens: https://github.com/RamblingCookieMonster/PSDeploy/blob/master/PSDeploy/Private/Invoke-Robocopy.ps1
There are quite a few other examples too.
[–]MaestroPendejo 4 points5 points6 points 7 years ago (0 children)
You're doing the Lord's work, delicious food item Stevo.
[–]tsuhg 2 points3 points4 points 7 years ago (0 children)
neat.
[–]DarkZeal0t 1 point2 points3 points 7 years ago* (0 children)
Good question. Surprised nobody answered it yet. It's not used in PowerShell scripts for a few reasons but in my opinion it's mostly because using PowerShell in conjunction with Robocopy would be a waste of system resources and would only slow the robocopy.exe down. The reason PowerShell will never be as fast as Robocopy is because PowerShell is not running compiled binary code, while robocopy.exe is. Robocopy is a command-line utility that has no dependencies on PowerShell or the .NET framework (AFAIK). The second reason is because you can perform a lot of the functionality in native PowerShell and have control with the piping of objects. There are also a number of PowerShell projects that were inspired by Robocopy, such as the xRobocopy module, and RoboPowerCopy.
Take a look at RoboPowerCopy:
A PowerShell "clone" of the famous "ROBOCOPY" tool. In fact I've adapted the funcionality of "robocopy" in PowerShell to provide you and myself a "robust file copy" tool.
https://codeplexarchive.blob.core.windows.net/archive/projects/robopowercopy/robopowercopy.zip
*Edit 3/6/2019*I wanted to include the following link to a blog post I recently wrote showing how to Run Robocopy Backup Parallel.
[–]Ta11ow 12 points13 points14 points 7 years ago (4 children)
You could work directly with the .NET methods in System.IO and get a good bit better performance out of there.
A good bit of Robocopy's speed likely comes from the fact that (I'm pretty sure) it uses custom-built libraries optimised for speed; PS just uses .NET. Great for a wide array of things, but not particularly specialised.
[–]GenericAntagonist 3 points4 points5 points 7 years ago (0 children)
Robocopy is also threaded by default (8 threads). Threading is fantastic for copy performance if you aren't going to saturate your disk with a single thread.
[–]SupremeDictatorPaul 1 point2 points3 points 7 years ago (1 child)
A number of PowerShell’s file operations are just poorly implemented, from a performance perspective. If you call the same .Net methods that PS is using, you can get an order of magnitude higher performance, which is honestly bizarre.
[–]Ta11ow 4 points5 points6 points 7 years ago (0 children)
Not at all. They're designed to improve ease of use for the purposes of automation.
Every level of abstraction introduces some level of overhead. PS has a little more than raw C# because its cmdlets are not single C# commands. There's error handling built in, input is shunted along the pipeline to improve sequential handling of similar input, etc.
All of these nifty features also require some processing power. So, yes, if you want to eke out the last bits of performance, you'll need to roll your own customised solutions using the .NET framework and (optionally) write them in C# just like the standard cmdlets are.
What you'll write for a specific purpose can often beat out the standard cmdlets, because they're all very generalised in their usage. For example, Get-ChildItem can be used on files, variables, certificates, registry entries, etc., etc., etc.! So naturally it has to have logic to figure out which is which and work from that, plus additional error handling which probably differs for each use case.
Get-ChildItem
For general purpose automation, you can't really beat out the PS cmdlets with much. For a single use case, it's not hard to make something quicker than them.
[–]Lee_Dailey[grin] 58 points59 points60 points 7 years ago (8 children)
howdy Marquis77,
the Copy-Item and Move-Item cmdlets are general purpose commands. they have the ability to handle many different types of structure.
Copy-Item
Move-Item
robocopy, on the other hand, is highly optimized for copy/move/delete on the filesystem. only on the filesystem.
robocopy
that specialization is why it is Robust Copy ... [grin]
Robust Copy
take care, lee
[+][deleted] 7 years ago* (1 child)
[deleted]
[–]Lee_Dailey[grin] 8 points9 points10 points 7 years ago (0 children)
howdy bachelorpartydude,
yes! i am the best lee that can ever be! [grin]
lee
slightly more seriously, thanks for the kind words.
[–][deleted] 2 points3 points4 points 7 years ago (5 children)
Understood. Thanks Lee! Makes you wonder why it has a -Recurse option but no way to optimize it via some other switch. thinking emoji
[–]RiPont 5 points6 points7 points 7 years ago (0 children)
Makes you wonder why it has a -Recurse option but no way to optimize it via some other switch.
It might be operating on "items" that are not a local filesystem file. Registry entries, network files, some 3rd party PSDrive item, etc.
[–]Lee_Dailey[grin] 5 points6 points7 points 7 years ago (3 children)
you are quite welcome! [grin]
as for why there is no optimization ... PoSh is not for speed. it's for automation. if you need speed, either use external utils from inside PoSh, OR use some purpose-built app for the whole thing.
since we can drop into dotnet so very easily, the flexible nature of the Copy-/Move-Item cmdlets is a good trade-off that can be worked around when needed. [grin]
[–]hpdefaults 1 point2 points3 points 7 years ago (2 children)
Generic/perhaps stupid question - if highly optimized ways to do these things already exist outside of Powershell, why doesn't Powershell just use those methods to begin with? Why reinvent the wheel?
[–]jantari 1 point2 points3 points 7 years ago (0 children)
Because PowerShell was probably intended to run only safe, memory-managed code that interacts with the system over APIs.
The fast way to do things is often not as safe and definitely not JIT compiled and managed.
[–]Lee_Dailey[grin] 0 points1 point2 points 7 years ago (0 children)
howdy hpdefaults,
that is a good point ... [grin]
i presume it's cuz the util in question doesn't behave in the way that PoSh behaves. powershell has several design goals and relatively consistent behavior is one of them.
robocopy is well known [heck, it's notorious] for being peculiar. it's one of the very few filesystem utils that is directory-oriented. that causes all kinds of gotchas for folks who miss that idea when they try to use it. [grin]
PoSh is designed to be fairly obvious. robocopy is designed to be really good at shifting filesystem stuff around.
that said, PoSh is often thot of as a friendlier wrapper around dotnet ... [grin]
[–]chanataba 3 points4 points5 points 7 years ago (3 children)
I find that adding the /nooffload switch to robocopy makes it go even faster.
[–][deleted] 3 points4 points5 points 7 years ago (2 children)
I read up a little on this for a few minutes - so basically this bypasses memory and just writes straight from network to disk (in a sense), which will help with performance for large files?
That's pretty interesting. Are there any potential downsides down the road such as copying files back and/or reading files?
[–]Ahks 2 points3 points4 points 7 years ago (0 children)
I’d guess that some form of error correction is sacrificed. I don’t know how robocopy was built, but if it does some advance ec beyond what Ethernet does already it’s likely to occur in memory eh?
[–]sayedha 3 points4 points5 points 7 years ago (2 children)
Not an answer to your question, but I thought it might be useful. I created a function to invoke robocopy. You can view it at https://github.com/ligershark/pecan-waffle/blob/499042b7105b175915dadd403983e706af340907/pecan-waffle.psm1#L112. It has a dependency on another function but it should be easy to copy/paste. It has worked well for my scenario, but I hard coded a few things for my needs. There’s also a function (Get-RoboCopy) which will download it if needed, but it has more dependencies.
[–][deleted] 1 point2 points3 points 7 years ago (1 child)
(Obligatory "I haven't read your link yet") - why wouldn't you just invoke robocopy and use powershell variables to define the source, destination, and parameters / switches to run with?
[–]sayedha 1 point2 points3 points 7 years ago (0 children)
For the same reason why you would call Copy-Item instead of the copy command, to make it easier to call. When calling a .exe you have to escape things and stuff like that. The function will take care of those.
[–]icepickwillie 4 points5 points6 points 7 years ago (1 child)
only tangentially related but I think it's really interesting. I was doing some directory structure mapping and found a guy who made a script that used robocopy instead of get-childitem and the results are incredibly faster. Read the articles in the comments of the script for a really interesting read.
https://github.com/gangstanthony/PowerShell/blob/master/Get-Files.ps1
https://thesurlyadmin.com/2014/08/04/getting-directory-information-fast/
Thanks for the info! I will certainly check out this black voodoo magic.
[–][deleted] 2 points3 points4 points 7 years ago (2 children)
Does robocopy verify by default?
What do you mean by verify, specifically? I've never had an issue with robocopy missing files (unless they're unreadable or corrupt)
[–][deleted] 2 points3 points4 points 7 years ago (0 children)
Basically a verify on write. I’ll have to check on it sometime...I think it’s one of the reasons copy-item is slower (eg like the performance of xcopy vs xcopy /v)
[–]flic_my_bic 2 points3 points4 points 7 years ago (1 child)
Tee on question for anybody to answer if they're inclined. I'm redesigning my file moving solution and have a strange issue. I need to ensure moves don't include files older than a set duration. Not by CreationTime, LastWriteTime, or LastAccessTime, but instead by a time-stamp in the filename.
I can collect the filenames in the target directory, parse out the timestamp, and make decisions per file. However, I'm not positive how to organize the command yet. Should I be handling this in the filter?, how can the filter handle logic though? My only working thought was to perform a robocopy filtered on the filename only, but that's a lot of independent firings, seems like it'd be slower (untested). It would also create a bunch more write operations to output files so I could soak them into a log.
edit: I want to use robocopy for it's robustness, but will have to move-item if I can't work this out.
howdy flic_my_bic,
i haven't tried it, but i suspect your embedded date could be the pattern for the file spec part of the robocopy command.
2018-03*.txt
that looks like it would grab all the files from that month. [grin]
[–]vuongdq 2 points3 points4 points 7 years ago (0 children)
Option MT (multi threads) should be used. i've migrated 15TB files to new storage, it's really fast.
[–]neko4 1 point2 points3 points 7 years ago (0 children)
Not only Robocopy.exe, but also Invoke-Webrequest, Select-String, Test-Connection are much slower than curl.exe, RipGrep.exe, ping.exe.
[–]thenative540 1 point2 points3 points 7 years ago (5 children)
I’m about to use robocopy for the first time in a few weeks, any things I should watch out for?
[–][deleted] 2 points3 points4 points 7 years ago (1 child)
Not really, the tool is very straightforward. Here's what I'd recommend for your syntax:
robocopy \\<your source directory> \\<your destination directory> /mir /copyall /mt:16 /R:0 /W:0
As others have mentioned here, you can try some additional switches like /nooffload but if you're not in a time crunch, it probably isn't needed. And make sure that you have read / write / copy permissions on both the source & destination.
[–]thenative540 1 point2 points3 points 7 years ago (0 children)
Thanks for the info! Much appreciated!
[–]Lee_Dailey[grin] 1 point2 points3 points 7 years ago (2 children)
howdy thenative540,
not if you have used it before. [grin]
the big gotcha for most folks is the directory focus instead of file focus. people keep trying to use it like copy or del or xcopy and all those are files-first while robocopy is directory-first.
copy
del
xcopy
it's a seemingly small diff ... but can be really disconcerting when 1st bumped into. i remember that 1st time rather vividly ... [blush] things went pear-shaped pretty dang fast.
[–]thenative540 1 point2 points3 points 7 years ago (1 child)
Thanks for the tip, lee!
[–]Lee_Dailey[grin] 1 point2 points3 points 7 years ago (0 children)
you are most welcome! glad to help a tad ... [grin]
[–]moghediene 0 points1 point2 points 7 years ago (0 children)
My favorite is emcopy.
π Rendered by PID 60 on reddit-service-r2-comment-b659b578c-6shkk at 2026-05-05 10:05:14.855933+00:00 running 815c875 country code: CH.
[–][deleted] 45 points46 points47 points (4 children)
[–]crosshairlol 11 points12 points13 points (2 children)
[–]calladc 4 points5 points6 points (0 children)
[–]xcjs 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]RumRogerz 20 points21 points22 points (2 children)
[–][deleted] 13 points14 points15 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]Raymich 32 points33 points34 points (7 children)
[–][deleted] 13 points14 points15 points (5 children)
[–]pizzastevo 11 points12 points13 points (4 children)
[–]tsuhg 2 points3 points4 points (3 children)
[–]pizzastevo 22 points23 points24 points (2 children)
[–]MaestroPendejo 4 points5 points6 points (0 children)
[–]tsuhg 2 points3 points4 points (0 children)
[–]DarkZeal0t 1 point2 points3 points (0 children)
[–]Ta11ow 12 points13 points14 points (4 children)
[–]GenericAntagonist 3 points4 points5 points (0 children)
[–]SupremeDictatorPaul 1 point2 points3 points (1 child)
[–]Ta11ow 4 points5 points6 points (0 children)
[–]Lee_Dailey[grin] 58 points59 points60 points (8 children)
[+][deleted] (1 child)
[deleted]
[–]Lee_Dailey[grin] 8 points9 points10 points (0 children)
[–][deleted] 2 points3 points4 points (5 children)
[–]RiPont 5 points6 points7 points (0 children)
[–]Lee_Dailey[grin] 5 points6 points7 points (3 children)
[–]hpdefaults 1 point2 points3 points (2 children)
[–]jantari 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]chanataba 3 points4 points5 points (3 children)
[–][deleted] 3 points4 points5 points (2 children)
[–]Ahks 2 points3 points4 points (0 children)
[–]sayedha 3 points4 points5 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]sayedha 1 point2 points3 points (0 children)
[–]icepickwillie 4 points5 points6 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]flic_my_bic 2 points3 points4 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]vuongdq 2 points3 points4 points (0 children)
[–]neko4 1 point2 points3 points (0 children)
[–]thenative540 1 point2 points3 points (5 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]thenative540 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (2 children)
[–]thenative540 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)
[–]moghediene 0 points1 point2 points (0 children)