Posted On January 16, 2020

PowerShell: Get MD5 Hashing Signature of a File

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Get MD5 Hashing Signature of a File
# Get-MD5-Hash.ps1

# Get-FileHash (PowerShell 4.0+) replacement for Windows 2008. Forward-compatible.
function getMd5{
param(
$file,
$md5 = (New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider)
)

# $hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($file))); # This line has the issue of 2GB file size limit
try{
$stream = [System.IO.File]::Open("$file",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read);
$hash = [System.BitConverter]::ToString($md5.ComputeHash($stream));
$stream.Dispose();
$stream.Close();
return $hash;
}
catch{
return $null;
}
}

# This is an alternative method, utilizing certutil.exe being bundled into the Windows OS for purposes of SSL encryption/decryption
function getHash{
param(
$file = "C:\temp\somefile.txt"
)
$hash=(certutil -hashfile $file)[1]; #'certutil -hashfile $file MD5' command doesn't work on Windows 2008
return $hash
}

$file="D:\somefile.exe"
getMd5 -file $file

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Change IP of Remote Windows

# Change-IP-Remote-Windows.ps1$oldIP="192.168.40.135"$newIP="192.168.40.136"$netmask="255.255.255.0"$gateway="192.168.40.1"$dnsServers=@("10.10.8.1","10.10.18.1")function changeIpRemoteComputer{ Param ( [string]$oldIP, [string]$newIP, [string]$newSubnetMask = "255.255.255.0", [string]$newDefaultGateway, [array]$newDNS = @("8.8.8.8","4.4.2.2") )…

Basic HTML and HTML5: Nest an Anchor Element within a Paragraph

<h2>CatPhotoApp</h2><main><a href="http://freecatphotoapp.com" target="_blank">cat photos</a><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."><p>Kitty ipsum dolor…

PowerShell: Check DotNet Framework on Windows

function checkDotNetVersions{ # General info: # - Powershell 2.0 latest SSL version support is TLS…