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.

    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% & goto :done
    

    A solution with external command would be:

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

    It is important to remember that if you put the FOR command in a batch file, you need to put %%a and not %a.

    In PowerShell, the solution is very simple:

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