PoshCode Logo PowerShell Code Repository

Hash Checker On One Line by Joel Bennett 34 months ago
View followups from Joel Bennett | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/988"></script>download | new post

Check and md5 or sha1 hash in a “single line” of PowerShell.

  1. # to make it readable, I've wrapped the lines, but you can remove all the line breaks:
  2. &{
  3.   PARAM($FileName,$HashFileName)
  4.   ((Get-Content $HashFileName) -match $FileName)[0].split(" ")[0] -eq
  5.   [string]::Join("", (
  6.     [Security.Cryptography.HashAlgorithm]::Create(
  7.       ([IO.Path]::GetExtension($HashFileName).Substring(1).ToUpper())
  8.     ).ComputeHash(
  9.       [IO.File]::ReadAllBytes( (Convert-Path $FileName)
  10.     )
  11.   ) | ForEach { "{0:x2}" -f $_ }))
  12. # Note that the last line here includes the PARAMETERS: FileName and HashFileName
  13. } npp.5.3.1.Installer.exe npp.5.3.1.release.md5

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me