1

The following two commands using the Get-FileHash cmdlet seem to give the same result (md5 hashes for all files in a directory and its subdirectories). I was wondering if there is any difference between piping in the list of file paths and using round brackets to the Get-FileHash cmdlet aside from the number of characters?

Get-FileHash -Algorithm MD5 -Path (Get-ChildItem "*.*" -Recurse)

Get-ChildItem "*.*" -Recurse | Get-FileHash -Algorithm MD5

Also, I tried timing the commands with Measure-Command a dozen times or so (based on this question Timing a command's execution in PowerShell; I don't know of a more statistically significant approach in PowerShell) -- on the same small directory on my system, the round bracket version often takes 8 to 9 milliseconds and the piped version takes 9 to 10 milliseconds.

Measure-Command { Get-FileHash -Algorithm MD5 -Path (Get-ChildItem "*.*" -Recurse) }

Measure-Command { Get-ChildItem "*.*" -Recurse | Get-FileHash -Algorithm MD5 }
Stacey
  • 81
  • 2
  • 9
  • the 1st version reads the files in the higher-priority perens section and puts that into the `-Path` parameter value. the 2nd reads the fileinfo and pipes that to the cmdlet where it is accepted `byName` or perhaps `byValue` into the `-Path` parameter. ///// with a large enuf set of files, the 1st will be faster since it avoids both the pipeline [slow-ish] and the parsing needed to assign values to the inferred parameter. – Lee_Dailey Jun 10 '19 at 04:28
  • Off the top of my head, Piping will pass each file object to the `Get-FileHash` function, where as the Bracketed version of the command will run through an array of objects. I am confirming this now with a larger sample size. – Drew Jun 10 '19 at 04:29

1 Answers1

0

Ran on a larger sample size, Take this for what you will. It appears it is faster to assign the GCI to a variable then use that as the Get-FileHash param.

Also as mentioned in the comments. Bracketed is an array, where as piping will 'push' each object to the function. Piping is generally slower.

Ran these numbers around 15 times and kept the last results because they were pretty similar.

(Get-ChildItem "*.*" -Recurse).Count
(Measure-Command { Get-FileHash -Algorithm MD5 -Path (Get-ChildItem "*.*" -Recurse) }).TotalSeconds
(Measure-Command { Get-ChildItem "*.*" -Recurse | Get-FileHash -Algorithm MD5 }).TotalSeconds
(Measure-Command { $Test = Get-ChildItem "*.*" -Recurse}).TotalSeconds + (Measure-Command {(get-FileHash -Algorithm MD5 $Test)}).TotalSeconds

# Total Files: 5244
# Seconds to run Bracketed GCI: 18.3848352
# Seconds to run Piped GI: 19.751385
# Seconds to run GCI to Object + Paramed hash: 17.5382904 (1.3471413 + 16.1911491)
Drew
  • 3,814
  • 2
  • 9
  • 28