windowscmdmd5

How to get the file's MD5 Hash in windows?


How do I get the file's md5 hash in windows using the command line? I only want the raw MD5 hash without all the extra text. I tried asking chatgpt but it gave me half working command.

for /f "skip=1 tokens=1" %a in ('certutil -hashfile "path\to\your\file" MD5') do @echo %a & goto :done

It still prints the CertUtil: -hashfile command completed successfully. which I don't want it appearing


Solution

  • It will be simpler if you throw the result into a variable. There is no need for the tokens parameter. This solution does not use external command, can be executed in just a single line and works only in the CMD console.

    @set "_dummy_=" & for /f "skip=1" %a in ('certutil -hashfile "path\to\your\file" MD5') do @if not defined _dummy_ set "_dummy_=%a" & echo %a
    

    For a batch file, it is necessary that the percent sign is doubled in the variable %a. In this case, it can be executed in multiple lines.

    @set "hash="
    @for /f "skip=1" %%a in ('certutil -hashfile "path\to\your\file" MD5') do @if not defined hash set "hash=%%a"
    @echo %hash%
    

    A solution with external command would be:

    for /f %a in ('certutil -hashfile "path\to\your\file" md5 ^| find /V ":"') do echo %a
    

    The solution was edited following the teachings of @mofi.

    In PowerShell, the solution is very simple:

    ( Get-FileHash 'path\to\your\file' -Algorithm 'MD5' ).Hash