Skip to content

Instantly share code, notes, and snippets.

@Wind010
Last active April 28, 2021 19:00
Show Gist options
  • Select an option

  • Save Wind010/8885df9d1b824b6389a6133e6b9bcaf3 to your computer and use it in GitHub Desktop.

Select an option

Save Wind010/8885df9d1b824b6389a6133e6b9bcaf3 to your computer and use it in GitHub Desktop.
Powershell script to diff two files with SHA256 hash.
<#
.DESCRIPTION
Powershell script to diff two files with SHA256 hash.
.OUTPUTS
System.String. Add-Extension returns a string with the extension or file name.
.EXAMPLE
PS> .\AreSameFile.ps1 .\deployed.json .\expected_different.json
#>
param(
[Parameter(Mandatory=$true)][string] $file1,
[Parameter(Mandatory=$true)][string] $file2,
[string] $logPath = ""
)
[Console]::ResetColor()
# Get the file hashes
$fileHash1 = Get-FileHash $file1 -Algorithm "SHA256"
$fileHash2 = Get-FileHash $file2 -Algorithm "SHA256"
$fileHash1 | Format-List
$fileHash2 | Format-List
if ($fileHash1.Hash -eq $fileHash2.Hash)
{
$host.UI.RawUI.ForegroundColor = "Green"
[string] $msg = "Source File Hash: $($fileHash1.Hash) is EQUAL TO Destination File Hash: $($fileHash2.Hash)."
if ([string]::IsNullOrWhitespace($logPath) -eq $false)
{
Add-Content -Path $logPath -Value $msg
}
else
{
Write-Host $msg
}
exit 0
}
$host.UI.RawUI.ForegroundColor = "Red"
[string] $msg = "Source File Hash: $($fileHash1.Hash) does not equal Existing Destination File Hash: $($fileHash2.Hash) the files are NOT EQUAL."
if ([string]::IsNullOrWhitespace($logPath) -eq $false)
{
Add-Content -Path $logPath -Value $msg
}
else
{
Write-Host $msg
}
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment