How to Get the MD5 Hash of a File in the Terminal
An MD5 hash is a 32-character fingerprint of a file's contents: the same contents always give the same hash, and changing even one character gives a different one. On macOS, md5 <file> prints it, md5sum <file> prints it in the format Linux uses, and in PowerShell, Get-FileHash -Algorithm MD5 <file> does the same.
This guide hashes three small text files, with real output from macOS 26 and PowerShell 7.6.10.
Syntax
md5 [-q] <file>...
md5sum <file>...
Get-FileHash -Algorithm MD5 <file>
| Part | What it means |
|---|---|
md5 | The macOS command for MD5 hashes |
-q | Optional. Prints only the hash, without the file's name |
md5sum | The same hash, in the format of the GNU command of the same name, which Linux has. macOS has it too |
<file>... | One or more files, separated by spaces |
Get-FileHash | The PowerShell command for a file's hash |
-Algorithm MD5 | Which kind of hash to make. Without it, Get-FileHash makes a SHA-256 hash |
<file> | In PowerShell, one file, or several separated by commas |
The macOS manual page, man md5, describes these commands as tools that calculate a message-digest fingerprint (checksum) for a file, and says md5sum has options and an output format that match the GNU commands of the same name. Microsoft's documentation describes Get-FileHash in one line: Computes the hash value for a file by using a specified hash algorithm.
The Example
A folder, md5-demo, holds three small text files: notes.txt with the line Buy milk, notes-copy.txt, an exact copy of it, and notes-new.txt with the line Buy milk and bread. You hash them and compare the hashes.
Step 1: Create the Three Files
- bash / zsh
- PowerShell
mkdir md5-demo
cd md5-demo
echo "Buy milk" > notes.txt
cp notes.txt notes-copy.txt
echo "Buy milk and bread" > notes-new.txt
mkdir creates the folder and cd moves into it. Each echo writes its line to a new file, and cp copies notes.txt. None of them prints anything.
New-Item -ItemType Directory -Path md5-demo | Out-Null
Set-Location md5-demo
Set-Content notes.txt "Buy milk"
Copy-Item notes.txt notes-copy.txt
Set-Content notes-new.txt "Buy milk and bread"
New-Item creates the folder, and | Out-Null hides the details it would print. Set-Location moves into the folder, each Set-Content writes its line to a new file, and Copy-Item copies notes.txt.
On Windows, PowerShell can end each line with two characters, a carriage return and a line feed, instead of one, so your hashes can differ from the ones below. A copy still has the same hash as its original.
Step 2: Get a File's MD5 Hash on macOS with md5
md5 notes.txt
MD5 (notes.txt) = 33a737dc0a68f8d623a4d53f935d6650
md5 prints the kind of hash, the file's name in brackets, and the hash: 32 characters, each a digit or a letter from a to f.
To get only the hash, for example to use it in a script, add -q:
md5 -q notes.txt
33a737dc0a68f8d623a4d53f935d6650

The manual calls -q quiet mode: only the checksum is printed out.
Step 3: Get It in the Linux Format with md5sum
md5sum notes.txt
33a737dc0a68f8d623a4d53f935d6650 notes.txt

The same hash, with the hash first, then two spaces and the file's name. That's the format of md5sum on Linux, so a command written this way works on both.
Step 4: Get It in PowerShell with Get-FileHash
Get-FileHash -Algorithm MD5 notes.txt
Algorithm Hash Path
--------- ---- ----
MD5 33A737DC0A68F8D623A4D53F935D6650 /Users/you/source/md5-demo/notes.txt
PowerShell prints a table: the kind of hash, the hash, and the file's full path. The hash is the same one, in capital letters. A hash is written in hexadecimal, where a and A stand for the same value, so 33a737dc… and 33A737DC… are the same hash.
To get only the hash, put the command in brackets and add .Hash:
(Get-FileHash -Algorithm MD5 notes.txt).Hash
33A737DC0A68F8D623A4D53F935D6650

Get-FileHash returns an object with three properties, Algorithm, Hash and Path, and .Hash reads one of them.
Step 5: Compare Files by Their Hashes
Give the command all three files:
md5 notes.txt notes-copy.txt notes-new.txt
MD5 (notes.txt) = 33a737dc0a68f8d623a4d53f935d6650
MD5 (notes-copy.txt) = 33a737dc0a68f8d623a4d53f935d6650
MD5 (notes-new.txt) = 5b3b46ace339a564ed4d83ae12a53472
md5sum notes.txt notes-copy.txt notes-new.txt
33a737dc0a68f8d623a4d53f935d6650 notes.txt
33a737dc0a68f8d623a4d53f935d6650 notes-copy.txt
5b3b46ace339a564ed4d83ae12a53472 notes-new.txt

In PowerShell, separate the files with commas:
Get-FileHash -Algorithm MD5 notes.txt, notes-copy.txt, notes-new.txt
Algorithm Hash Path
--------- ---- ----
MD5 33A737DC0A68F8D623A4D53F935D6650 /Users/you/source/md5-demo/notes.txt
MD5 33A737DC0A68F8D623A4D53F935D6650 /Users/you/source/md5-demo/notes-copy.txt
MD5 5B3B46ACE339A564ED4D83AE12A53472 /Users/you/source/md5-demo/notes-new.txt
The original and its copy have the same hash, because their contents are the same, even though their names differ. notes-new.txt has a completely different hash, although it only has two more words. Microsoft's documentation puts it this way: If the hash values of two files are identical, the contents of the files are also identical.
Step 6: See What Happens When the File Isn't There
A typo, note.txt instead of notes.txt:
md5 note.txt
md5: note.txt: No such file or directory
md5sum note.txt
md5sum: note.txt: No such file or directory

Get-FileHash -Algorithm MD5 note.txt
Get-FileHash: Cannot find path '/Users/you/source/md5-demo/note.txt' because it does not exist.
Each command names the file it couldn't find. Both md5 and md5sum exit with status 1 here, which the manual explains: they exit 0 on success, 1 if at least one of the input files could not be read.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Get-FileHash notes.txt, without -Algorithm MD5 | You get a SHA-256 hash, 64 characters long, not the MD5 hash | Add -Algorithm MD5 |
Comparing a PowerShell hash with an md5 hash letter by letter | The letters differ in case, so the hashes look different | Compare them without regard to case: they're the same hash |
| Using MD5 to protect files from tampering | MD5 isn't secure against attack | Use MD5 only to spot changes; use SHA-256 for security |
FAQ
Which hash does Get-FileHash make without -Algorithm?
SHA-256. (Get-FileHash notes.txt).Algorithm printed SHA256, and Microsoft's documentation says: If no value is specified, or if the parameter is omitted, the default value is SHA256.
Is MD5 safe to use?
To spot changes, yes. For security, no. Microsoft's documentation says MD5 and SHA1, which are no longer considered secure, should only be used for simple change validation, and the macOS manual warns that MD5 is vulnerable to practical collision attacks: someone can make two different files with the same hash on purpose.
Should I use md5 or md5sum on macOS?
Either gives the same hash. md5sum prints the format Linux uses, so it's the better choice for commands that also run on Linux.
Conclusion
md5 notes.txt printed the MD5 hash of notes.txt, md5 -q printed only the hash, md5sum printed it in the Linux format, and Get-FileHash -Algorithm MD5 printed it in PowerShell, in capital letters. A file and its copy had the same hash, and a changed file had a new one, which is what makes a hash useful for spotting changes.
Additional Resources
- Get-FileHash (Microsoft Learn):
-Algorithm,-Pathand what a hash value is. - md5sum invocation (GNU Coreutils): the Linux command.
man md5in the macOS Terminal: every option ofmd5andmd5sum.
