Skip to content

Instantly share code, notes, and snippets.

@Luen
Created July 29, 2024 02:10
Show Gist options
  • Save Luen/504bdbb3bc4fa896861011bacb4f077e to your computer and use it in GitHub Desktop.
Save Luen/504bdbb3bc4fa896861011bacb4f077e to your computer and use it in GitHub Desktop.
Compare files in Windows Powershell
$originalFile = "C:\Users\luenw\Downloads\original.img"
$downloadedFile = "C:\Users\luenw\Downloads\downloaded.img"
$originalHash = Get-FileHash -Path $originalFile -Algorithm MD5
$downloadedHash = Get-FileHash -Path $downloadedFile -Algorithm MD5
if ($originalHash.Hash -eq $downloadedHash.Hash) {
Write-Output "Checksum: The files are identical."
} else {
Write-Output "Checksum: The files are different."
}
$chunkSize = 1024 * 1024 # 1MB chunks
# Open files for reading
$originalStream = [System.IO.File]::OpenRead($originalFile)
$downloadedStream = [System.IO.File]::OpenRead($downloadedFile)
$bufferOriginal = New-Object byte[] $chunkSize
$bufferDownloaded = New-Object byte[] $chunkSize
$identical = $true
try {
while ($true) {
$bytesReadOriginal = $originalStream.Read($bufferOriginal, 0, $chunkSize)
$bytesReadDownloaded = $downloadedStream.Read($bufferDownloaded, 0, $chunkSize)
if ($bytesReadOriginal -ne $bytesReadDownloaded) {
$identical = $false
break
}
if ($bytesReadOriginal -eq 0) {
break
}
for ($i = 0; $i -lt $bytesReadOriginal; $i++) {
if ($bufferOriginal[$i] -ne $bufferDownloaded[$i]) {
$identical = $false
break
}
}
if (-not $identical) {
break
}
}
} finally {
$originalStream.Close()
$downloadedStream.Close()
}
if ($identical) {
Write-Output "Chucks: The files are identical."
} else {
Write-Output "Chucks: The files are different."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment