3

In Win10, looking for way to pipe the output from a DIR command at the Command Prompt or in a Batch File to serve as input to the CERTUTIL command. IOW, I want to get the MD5 hash for all of the files matched by a DIR command.

The following command does produce a bare list of all the files in the E:\Temp folder:

C:\Users\RAS>dir "E:\Temp" /b

But when I pipe that as shown in the following command, I get an error message:

C:\Users\RAS>dir "E:\Temp" /b | CertUtil -hashfile %~f1 MD5
CertUtil: -hashfile command FAILED: 0x80070002 (WIN32: 2 ERROR_FILE_NOT_FOUND)
CertUtil: The system cannot find the file specified.

The answer by user1686 at Windows Vista piping dir output into attrib command indicates that some commands (like ATTRIB) don't take file names as input, but I don't think that's relevant here.

The answer by Cliff Armstrong at get the hash of a string and eventually compare it to a hash looks promising, but I don't see how to pass each fully qualified file name to CERTUTIL. Also, I have no experience with PowerShell and would like to get a solution using a Batch File, if possible.

Thank you,

DW

2 Answers 2

3

Consider that Dir will not put his own output in the correct argument for using CertUtil and, CertUtil will not place Dir redirected input in the right place (correct position/argument order) to use it.

If you try to use the for /f loop with the where command, instead of dir /b (it does not result in the full file path), this will result in the full file path and you can use the output loop variable in Certutil:

  • In command line:
for /f tokens^=* %i in ('%__APPDIR__%where.exe "E:\Temp:*.*"')do @%__APPDIR__%CertUtil.exe -hashfile "%~i" MD5

rem :: Output  :: 

MD5 hash of E:\Temp\Turn Off LCD.exe:
3657b64bfa767cd1ce1ea3709053ea3b
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\LngVar.exe:
fc82a6b8fa5c24f6cbcb0f0dcbf85a2e
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\cocolor.exe:
d9a3def8f569afda41fb6e067c5f3df3
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\aria2c.exe:
80f598187166a8f95d86985ba0244257
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\cmdFocus.exe:
f90f8672fa57ba4e8f0a05dec3ede654
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\where.exe:
7b6f5b80b4db4ca0c0472625bcd0c981
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\SaveColor.exe:
0b24aa776ca4601bb39e6e529e73e7a6
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\Windows-ISO-Downloader.exe:
11532e016f68ef22ca96fa03020de789
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\sudoku.exe:
a3a946be19763b72f8aab6387079207a
CertUtil: -hashfile command completed successfully.
  • The same in bat/cmd file:
@echo off  

for /f tokens^=* %%i in ('%__APPDIR__%where.exe "E:\Temp:*.*
')do %__APPDIR__%CertUtil.exe -hashfile "%%~i" MD5"

rem :: Output :: 
MD5 hash of E:\Temp\Turn Off LCD.exe:
3657b64bfa767cd1ce1ea3709053ea3b
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\LngVar.exe:
fc82a6b8fa5c24f6cbcb0f0dcbf85a2e
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\cocolor.exe:
d9a3def8f569afda41fb6e067c5f3df3
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\aria2c.exe:
80f598187166a8f95d86985ba0244257
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\cmdFocus.exe:
f90f8672fa57ba4e8f0a05dec3ede654
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\where.exe:
7b6f5b80b4db4ca0c0472625bcd0c981
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\SaveColor.exe:
0b24aa776ca4601bb39e6e529e73e7a6
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\Windows-ISO-Downloader.exe:
11532e016f68ef22ca96fa03020de789
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\sudoku.exe:
a3a946be19763b72f8aab6387079207a
CertUtil: -hashfile command completed successfully.

If you only need the MD5 strings in output, add |find/v ":":

  • In command line:
@for /f tokens^=* %i in ('%__APPDIR__%where.exe "E:\Temp:*.*"')do @%__APPDIR__%CertUtil.exe -hashfile "%~i" MD5|find/v ":"

rem :: Output  :: 

3657b64bfa767cd1ce1ea3709053ea3b
fc82a6b8fa5c24f6cbcb0f0dcbf85a2e
d9a3def8f569afda41fb6e067c5f3df3
80f598187166a8f95d86985ba0244257
f90f8672fa57ba4e8f0a05dec3ede654
7b6f5b80b4db4ca0c0472625bcd0c981
0b24aa776ca4601bb39e6e529e73e7a6
11532e016f68ef22ca96fa03020de789
a3a946be19763b72f8aab6387079207a
  • In bat/cmd file:
@echo off 

for /f tokens^=* %%i in ('%__APPDIR__%where.exe "E:\Temp:*.*"
')do %__APPDIR__%CertUtil.exe -hashfile "%%~i" MD5|find/v ":"

rem :: Output  :: 

3657b64bfa767cd1ce1ea3709053ea3b
fc82a6b8fa5c24f6cbcb0f0dcbf85a2e
d9a3def8f569afda41fb6e067c5f3df3
80f598187166a8f95d86985ba0244257
f90f8672fa57ba4e8f0a05dec3ede654
7b6f5b80b4db4ca0c0472625bcd0c981
0b24aa776ca4601bb39e6e529e73e7a6
11532e016f68ef22ca96fa03020de789
a3a946be19763b72f8aab6387079207a

You also can do it more simple and recursively using For / r, same used in like linked question:

  • In command line:
@for /r E:\temp %i in (*)do @%__APPDIR__%CertUtil.exe -hashfile "%~i" MD5|find/v ":"
  • In bat/cmd file:
@echo off 

for /r E:\temp %%i in (*)do %__APPDIR__%CertUtil.exe -hashfile "%%~i" MD5|find/v ":"

0

You stated,

I want to get the MD5 hash for all of the files matched [...]

I was also trying to match hashes and was also getting the error

CertUtil: -hashfile command FAILED: 0x80070002 (WIN32: 2 ERROR_FILE_NOT_FOUND)

The solution I found was to download a hash checker app, which apparently did a good job. It extracted the hash from the document, compared it with the one I provided and found they matched.

I saw a few such apps in the Microsoft App store. The one I got had several options for hash types, including SHA256 and MD5.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 7, 2023 at 18:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .