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

Quick & Useful Snippet to Set SSL TLS Protocol of PowerShell

$requiredTls='Tls12' $availableSslProtocols=[enum]::getnames([net.securityprotocoltype]) if([Net.ServicePointManager]::SecurityProtocol -notin $requiredTls -and $requiredTls -in $availableSslProtocols){ [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::$requiredTls }

Function to list Domain Controllers

Wuick Script: # This function runs faster than the "Get-ADDomainController -Filter *" methodfunction listControllers{$domain =…

PowerShell: Set Autologon for Windows

Current Version: function setAutoLogon($username,$password){ $regWinlogon='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' $regKeyUsername='DefaultUserName' $regKeyPassword='DefaultPassword' $kegKeyAutologin='AutoAdminLogon' $regKeyDefaultDomain='DefaultDomain' function testCredential($username,$password){ # Get current…