Checking whether two files are identical in Powershell is pretty straight forward. We will be making use of the MD5 hash of each file, and comparing their values together, to determine if the two files are the same.
We will utilize the “Get-FileHash” Powershell command, to retrieve each file’s hash, then compare them using the “-eq” command.
Here is an example below. We have two file paths stored in the variables, “$filePathOne” and “filePathTwo”, which we grab the MD5 hash from, and then comparing them together. The resulting boolean, indicating if the files are the same, is stored in the “$filesAreSame” variable.
$filePathOne = "C:\TestFileOne.png"
$filePathTwo = "C:\TestFileTwo.png"
$filesAreSame = (Get-FileHash $filePathOne -Algorithm MD5).Hash -eq (Get-FileHash $filePathTwo -Algorithm MD5).Hash