Verifying if Two Files Are Truly Identical: Using SHA-256 in PowerShell

PowerShellカテゴリを表すパンダのイラスト PowerShell

About This Article
This article was created using an automated generation workflow leveraging generative AI. It is structured to review the Get-FileHash documentation from Microsoft Learn and the SHA-256 specifications from NIST, and to observe content differences using dummy files created under the TEMP directory.

Verification Status: 📘 Confirmed with official Microsoft and NIST specifications; not yet verified on a physical Windows machine.

Even if the filename and size are identical, it does not necessarily mean the contents are the same. By calculating the SHA-256 hash using PowerShell's Get-FileHash and comparing the two digests, you can easily verify the contents after backing up or copying.

Getting Started

Create dummy files exclusively within the TEMP directory.

$dir = Join-Path $env:TEMP 'papanda-hash-demo'
New-Item -ItemType Directory -Path $dir -Force | Out-Null

'PANDA-001' | Set-Content (Join-Path $dir 'a.txt') -NoNewline
'PANDA-001' | Set-Content (Join-Path $dir 'b.txt') -NoNewline

$hashA = Get-FileHash (Join-Path $dir 'a.txt') -Algorithm SHA256
$hashB = Get-FileHash (Join-Path $dir 'b.txt') -Algorithm SHA256

$hashA
$hashB
"Same = $($hashA.Hash -eq $hashB.Hash)"

The expected point of observation is Same = True.

View This

SHA-256 calculates a fixed-length digest from the file contents. Since it takes the byte sequence as input rather than the filename, identical contents will yield the exact same digest.

flowchart LR
    A[File A] --> C[SHA-256]
    B[File B] --> D[SHA-256]
    C --> E{digest比較}
    D --> E
    E -->|同じ| F[内容一致を強く確認]
    E -->|違う| G[内容は異なる]

Modifying a Single Location

b.txtModify just the last character of

'PANDA-002' | Set-Content (Join-Path $dir 'b.txt') -NoNewline

$hashB2 = Get-FileHash (Join-Path $dir 'b.txt') -Algorithm SHA256
"Same = $($hashA.Hash -eq $hashB2.Hash)"

.FalseThis time, it becomes

Hashing Is Not Encryption

SHA-256 is not a feature for keeping files secret. It is a one-way hash function that generates a digest from the contents. NIST defines SHA-256 in the Secure Hash Standard.

Because hash collisions exist as a theoretical concept, we cannot state unconditionally that "matching digests mean the files are mathematically identical." However, using matching SHA-256 hashes for standard file integrity checks provides an extremely strong validation method.

Difference from Size Comparison

Files of the same size can still have different contents. For example, ABC and XYZ both consist of three characters, but their contents are different.

'ABC' | Set-Content (Join-Path $dir 'a.txt') -NoNewline
'XYZ' | Set-Content (Join-Path $dir 'b.txt') -NoNewline

Get-Item (Join-Path $dir '*.txt') | Select-Object Name, Length
Get-FileHash (Join-Path $dir '*.txt') -Algorithm SHA256

While checking that "sizes are the same" serves as a fast preliminary check, it is distinct from verifying that "contents are the same."

Use Cases in the Workplace

  • Verifying that important files copied to a NAS match the originals

  • Performing checksum verification after restoring from a backup

  • Comparing distribution files received from vendors against official SHA-256 values

  • Classifying whether multiple files with the same name have identical contents

Because calculating hashes for a large number of files requires disk read time, you should check the count and file sizes before running the process. It is also important to ensure that the published official hash values themselves are obtained through a trusted channel.

Cleanup

Delete only the dummy files.

Remove-Item -LiteralPath $dir -Recurse -Force

$dirAlso, make sure to verify that the target is restricted to the TEMP directory you specified yourself.

Official and Primary Sources

Document information

Article title
Verifying if Two Files Are Truly Identical: Using SHA-256 in PowerShell
Published
Updated
Source
https://papanda925.com/?p=15621&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL