gangstanthony

fastest Get-ChildItem

May 27th, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://www.reddit.com/r/PowerShell/comments/4lavk7/getfiles_using_robocopy_dir_enumeratefiles_or/
  2.  
  3. $path = 'c:\temp'
  4.  
  5. $a1 = New-Object System.Collections.ArrayList
  6. $a2 = New-Object System.Collections.ArrayList
  7. $a3 = New-Object System.Collections.ArrayList
  8. $a4 = New-Object System.Collections.ArrayList
  9. $a5 = New-Object System.Collections.ArrayList
  10.  
  11. $null = 1..5 | % {
  12.     $a1.Add(
  13.         (Measure-Command {
  14.             # the built-in Get-ChildItem is probably still the fastest
  15.             Get-ChildItem $path -File -Recurse
  16.         }).Ticks
  17.     )
  18.     $a2.Add(
  19.         (Measure-Command {
  20.             # Checks hidden files by default
  21.             Get-Files $path -Method EnumerateFiles -File -Recurse
  22.         }).Ticks
  23.     )
  24.     $a3.Add(
  25.         (Measure-Command {
  26.             # somehow, cmd /c dir is faster than robocopy
  27.             Get-Files $path -Method Dir -File -Recurse
  28.         }).Ticks
  29.     )
  30.     $a4.Add(
  31.         (Measure-Command {
  32.             Get-Files $path -Method AlphaFS -File -Recurse
  33.         }).Ticks
  34.     )
  35.     $a5.Add(
  36.         (Measure-Command {
  37.             # robocopy is the slowest. faster than dir, but only when getting just the name
  38.             Get-Files $path -Method Robocopy -File -Recurse
  39.         }).Ticks
  40.     )
  41. }
  42.  
  43. ($a1 | measure -Sum).sum
  44. ($a2 | measure -Sum).sum
  45. ($a3 | measure -Sum).sum
  46. ($a4 | measure -Sum).sum
  47. ($a5 | measure -Sum).sum
Add Comment
Please, Sign In to add comment