PowerShell Genie

Compare a copied file with SHA256

Script 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Get-FileHash reads each file using SHA256. LiteralPath treats brackets in filenames literally.

The script

1PS> $sourceHash = Get-FileHash -LiteralPath '.\source.txt' -Algorithm SHA256 -ErrorAction Stop
2PS> $copyHash = Get-FileHash -LiteralPath '.\copy.txt' -Algorithm SHA256 -ErrorAction Stop
3PS> [pscustomobject]@{
4PS> SourceHash = $sourceHash.Hash
5PS> CopyHash = $copyHash.Hash
6PS> Match = $sourceHash.Hash -eq $copyHash.Hash }

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 SourceHash CopyHash Match
    2 ---------- -------- -----
    3 ABCDEF012345 ABCDEF012345 True
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    $sourceHash = Get-FileHash -LiteralPath '.\source.txt' -Algorithm SHA256 -ErrorAction Stop $copyHash = Get-FileHash -LiteralPath '.\copy.txt' -Algorithm SHA256 -ErrorAction Stop [pscustomobject]@{ SourceHash = $sourceHash.Hash CopyHash = $copyHash.Hash Match = $sourceHash.Hash -eq $copyHash.Hash }
    1. Stage 1$sourceHashPositional values or no named parameters

      How it works

      Get-FileHash reads each file using SHA256. LiteralPath treats brackets in filenames literally. ErrorAction Stop prevents a failed file read from continuing into a misleading comparison. The report retains both hash strings and a Boolean Match value.

      Adapting it

      Replace the two file paths with the source and copy you intend to compare. Keep both files stable during the check and use the same hash algorithm for both.

      Primary documentation

      Files, storage and hashes documentation

      Continue
      Genie guideCompare a copied file with SHA256

      Get-FileHash reads each file using SHA256. LiteralPath treats brackets in filenames literally.