Verifying File Hashes with PowerShell

I recently wanted to verify the hash of a file, named setup.exe, that was provided in a GnuPG signature file, named setup.txt.

I used the PowerShell code below to verify the expected SHA-256 hash contained within the setup.txt file against the actual hash of the setup.exe file. You can use this script by modifying the first two lines for your needs, i.e. the filenames.

This code/script was modified from https://www.christopherkeim.com/post/verify-file-hash-with-powershell.

Note that the GnuPG signature file, i.e. setup.txt, had the following format:

I also used regex help from:

  1. regex – Regular Expression to select everything before and up to a particular text – Stack Overflow.
  2. java – Getting the text that follows after the regex match – Stack Overflow