---
title: "How to Use the Get-FileHash PowerShell Cmdlet"
description: "Use Get-FileHash to generate a hash for a single file, an entire directory of files, or even generate hashes on streams of data using the Get-FileHash PowerShell cmdlet!"
canonical: "https://adamtheautomator.com/get-filehash/"
---

# How to Use the Get-FileHash PowerShell Cmdlet

> Use Get-FileHash to generate a hash for a single file, an entire directory of files, or even generate hashes on streams of data using the Get-FileHash PowerShell cmdlet!

Source: https://adamtheautomator.com/get-filehash/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Use the Get-FileHash PowerShell Cmdlet](https://adamtheautomator.com/wp-content/uploads/2021/03/The-PowerShell-Get-FileHash-Cmdlet-Can-Do-More-Than-you-Think.jpg)

# How to Use the Get-FileHash PowerShell Cmdlet

[![](https://secure.gravatar.com/avatar/27e51fdfdee0acb33447a19c501780e43dec078b24bd8d9d9c10fd1c11a6b21c?s=192&d=mm&r=g)Devin Rich](https://adamtheautomator.com/author/devin/)9 March 20217 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [What is a Hash?](#h-what-is-a-hash)
*   [Computing a Hash Value for a Single File](#h-computing-a-hash-value-for-a-single-file)
*   [Computing Hash Values for Files in a Directory](#h-computing-hash-values-for-files-in-a-directory)
*   [Recursively Generate Hash Values for All Files](#h-recursively-generate-hash-values-for-all-files)
*   [Adding a File Hash to Get-ChildItem Output via a Calculated Property](#h-adding-a-file-hash-to-get-childitem-output-via-a-calculated-property)
*   [Comparing All Files in Two Directories Using Hashes](#h-comparing-all-files-in-two-directories-using-hashes)
*   [Generate a Hash Value From a Stream of Data](#h-generate-a-hash-value-from-a-stream-of-data)
*   [Calculating the Hash of a File Split Into Multiple Files (Chunked Data)](#h-calculating-the-hash-of-a-file-split-into-multiple-files-chunked-data)
*   [Aside: Understanding Chunking and Streaming of Files](#h-aside-understanding-chunking-and-streaming-of-files)
*   [Next Steps](#h-next-steps)

Do you need to compare two files or make sure a file has not changed? The PowerShell cmdlet `[_Get-FileHash_](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash?view=powershell-7.1)` generates hash values both for files or streams of data. A hash is simply a function that converts one value into another. Sometimes the hash value may be smaller to save on space, or the hash value may be a checksum used to validate a file.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

In this article, you will learn several ways to use `Get-FileHash` as well as more details on hashing background and security best practices!

Related:[How to Create Checksums in Windows (Built-in and Third-Party Tools)](https://adamtheautomator.com/checksum-windows/)

## Prerequisites

Any version of PowerShell greater than 4, which includes both Windows PowerShell and PowerShell Core.

## What is a Hash?

What if you can create a simple value that represents a bit of data or file? A hash value or digital signature allows you to compare two sets of data. Hashes are considered consistent and functional because the same input will always create the same output.

Therefore a hash will be different if even a single character in the input is changed. Hashes are computed using a specific hashing algorithm, the method of computing the hash value.

Each hashing [_algorithm_](https://www.thinkautomation.com/eli5/what-is-an-algorithm-an-in-a-nutshell-explanation/) has specific situations that it is well suited for. Often there is a tradeoff between speed and security. An algorithm such as MD5 is fast, but with less complex hash values. On the other hand, SHA512 generates [_more complex hashes but is slower overall_](https://automationrhapsody.com/md5-sha-1-sha-256-sha-512-speed-performance/).

For security measures, MD5 is typically not recommended. Instead, try to use SHA512.

> _One important use of hashing is for signing data. Sometimes, two different inputs will produce the same output hash. This is called a hash collision. When anyone can predictably create hashing collisions for a specific algorithm, that [algorithm becomes considered less secure or “broken”](https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms)._

PowerShell’s `Get-FileHash` cmdlet supports a variety of algorithms as listed below.

*   PowerShell Core (version 6 and 7): `MD5`, `SHA1`, `SHA256`, `SHA384`, and `SHA512`
*   Windows PowerShell (version 4 through 5.1): `MACTripleDES`, `MD5`, `RIPEMD160`, `SHA1`, `SHA256`, `SHA384`, and `SHA512`

## Computing a Hash Value for a Single File

To become familiar with the `Get-FileHash` cmdlet, pass a single file to the command, as seen in the below example.

```powershell
Get-FileHash C:\Windows\write.exe
```

`Get-FileHash` will output the algorithm used, the hash value of the file, and the full path of the file that you specified, as shown below. The default value is `SHA256` for all versions of PowerShell, if no other algorithm is specified.

![Computing the hash value of a single file.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T111240.227.png)

Computing the hash value of a single file.

## Computing Hash Values for Files in a Directory

To generate hash values for every file in a directory, use wildcards (`*`) in the `Path` parameter. You may notice that no algorithm is specified.

Copy the command below and run from the root _C:\\_ directory to generate `SHA256` hash values for all executables in the _C:\\Windows_ directory.

```powershell
# The example below computes the hashes relative to the current directory
# notated by the "." before the "\".
Get-FileHash .\Windows\*.exe
```

![Compute hash values for all executables in the C:\\\\Windows directory.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T111628.772.png)

Compute hash values for all executables in the `C:\\Windows` directory.

### Recursively Generate Hash Values for All Files

To hash all files in a directory and its sub-directories, recursively list items via `[_Get-ChildItem_](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.1)` and pass the results to `Get-FileHash`. Although you are able to use the wildcard character with `Get-FileHash` this does not recursively traverse each sub-directory.

```powershell
Get-ChildItem -Path C:\py* -Recurse -Filter *.exe | Get-FileHash
```

![Recursively generating executable file hashes from all directories and sub-directories.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T111741.612.png)

Recursively generating executable file hashes from all directories and sub-directories.

## Adding a File Hash to `Get-ChildItem` Output via a Calculated Property

`Get-FileHash` by itself has limited output. When used in conjunction with `Get-ChildItem` many file properties are lost in the output. To keep the file information, create a [_calculated property_](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_calculated_properties?view=powershell-7.1) to dynamically add the file hash.

Related:[Back to Basics: Understanding PowerShell Objects](https://adamtheautomator.com/powershell-objects/)

The example below reads the _windows-version.txt_ example file, selects only the `Name` and `Length` property from the `Get-ChildItem` output, and adds the `Hash` calculated property.

```powershell
Select-Object uses a hashtable to create a calculated property. Define a "Name" and an
 "Expression", which operates on the current object in the pipeline.
 Get-ChildItem .\windows-version.txt | 
 Select-Object -Property Name,Length,
 @{name="Hash";expression={(Get-FileHash $_.FullName).hash}}
```

![Adding the file hash via a calculated property to the output of Get-ChildItem.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T111901.460.png)

Adding the file hash via a calculated property to the output of `Get-ChildItem`.

## Comparing All Files in Two Directories Using Hashes

Suppose you have two folders and need to find which files are different. You can compare all files in these folders by generating a hash with `Get-FileHash` for each file and then comparing them.

Building on what you have learned with calculated properties and `Get-FileHash`, the cmdlet `[_Compare-Object_](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.1)` compares the computed hash for each file and outputs any differences.

First, retrieve a set of executable files and computed hashes for use with `Compare-Object`.

```powershell
# Retrieve all executables directly located within the C:\Windows directory and compute a hash value for each.
$WindowsExesOnly = Get-ChildItem C:\Windows\*.exe | 
Select-Object -Property *,
@{name="Hash";expression={(Get-FileHash $_.FullName -Algorithm MD5).hash}}

# Retrieve all executables located in any directory from the C:\ drive, but not recursively, and compute a hash value for each.
$SecondLevelExes = Get-ChildItem C:\*\*.exe | 
Select-Object -Property *,
@{name="Hash";expression={(Get-FileHash $_.FullName -Algorithm MD5).hash}}
```

Next, supply the resulting file object arrays, `$WindowsExesOnly` and `$SecondLevelExes`, to `Compare-Object` using the `ReferenceObject` and `DifferenceObject` parameters. Run the following code and pass both the `Name` and `Hash` properties for comparison to `Compare-Object`. Using both the `Name` and `Hash` properties allows for identical files but with different file names.

```powershell
Compare-Object -Ref $WindowsExesOnly -Dif $SecondLevelExes -Property Name,Hash
```

The `Compare-Object` output shows the `SideIndicator` property, which indicates which array the non-identical files exist in. As shown below, six unique files are located in the `$SecondLevelExes` array.

![Using Compare-Object to retrieve unique executables compared across directories.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T112139.396.png)

Using `Compare-Object` to retrieve unique executables compared across directories.

What if you wanted to see the file length for the unique files? With the `PassThru` parameter, the original properties are carried through the pipeline with the `SideIndicator` property added to the original object.

```powershell
Compare-Object -Ref $WindowsExesOnly -Dif $SecondLevelExes -Property Name,Hash -PassThru | Select-Object SideIndicator,Name,Hash,Length
```

![Including all properties, such as Length, with the compared objects.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T112214.029.png)

Including all properties, such as `Length`, with the compared objects.

## Generate a Hash Value From a Stream of Data

So far you’ve learned how to find a file’s hash but `Get-FileHash` can do you one better. It can also get a hash of data that isn’t stored in a file, a stream of data.

Keep in mind that any difference, even an extra newline character, would cause the hash to change. One method to avoid any hashing issues is by converting the data to a stream of data that is passed to `Get-FileHash` via the `InputStream` parameter, as shown below.

```powershell
Create an in-memory stream of data with the [System.IO.MemoryStream] constructor.
 Retrieve the bytes of a string using the [System.Text.Encoding]::ASCII static method.
 $String = "Hello World"
 Get-FileHash -InputStream ([System.IO.MemoryStream]::New([System.Text.Encoding]::ASCII.GetBytes($String)))
```

![Viewing the resulting hash from a stream of data.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T112304.263.png)

Viewing the resulting hash from a stream of data.

In fact, you can turn any series of bytes into a `[_MemoryStream_](https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream?view=net-5.0)`, a series of data stored in RAM, for `Get-FileHash` to generate a hash from, not just strings!

## Calculating the Hash of a File Split Into Multiple Files (Chunked Data)

Suppose you have a large 5TB backup file and the resulting hash value to validate the file. Due to the size, the file was split into 100GB blocks of chunked data for storage flexibility.

One potential way to validate the original file is to combine all 100GB files together and verify the hash of the resulting file. Of course, 5TB is a lot of disk space!

Instead, you can combine individual file hash values. By combining the hash values, you skip the disk space-consuming step of combining the files. See the below code for an example of how to stream the chunked data into a combined hash value.

```powershell
# First, create a hash algorithm object using SHA256.
$Algorithm = [System.Security.Cryptography.HashAlgorithm]::Create("SHA256")

# Next, create a cryptographic stream of data using the SHA256 hash algorithm.
$CryptoStream = [System.Security.Cryptography.CryptoStream]::new(
    ([System.IO.Stream]::Null), 
    $Algorithm, 
    "Write"
)

# Retrieve each file and copy the data into the cryptographic stream.
foreach ($File in Get-ChildItem -File) {
    $FileStream = [io.file]::OpenRead($File.FullName)
    $FileStream.CopyTo($CryptoStream)
}

# Close all files and close out the cryptographic stream.
$FileStream.Close()
$CryptoStream.FlushFinalBlock()

# Combine all of the hashes as hexadecimal formats "X2" and join the values.
($Algorithm.Hash | ForEach-Object {$_.ToString("X2")}) -join ''
```

### Aside: Understanding Chunking and Streaming of Files

It’s not necessary to understand all the ins-and-outs of chunking and streaming data to compute file hashes, but really learn exactly how this all works!

To illustrate the nature of this chunking and streaming, imagine that your source file contains the values of `1` and `2`. In between each number, is a Windows newline (`\r\n`) as represented in the hex editor shown below.

![Viewing the source file in a hex editor.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T112414.569.png)

Viewing the source file in a hex editor.

This source file has been split into two files with the content `1` and `2` in them respectively. Viewing each file in a hex viewer shows that each file is three bytes with a number and the Windows newline.

![Viewing the chunked parts in a hex editor.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T112442.859.png)

Viewing the chunked parts in a hex editor.

As shown below, the source file hash and the hash of these two chunked files match.

![Demonstrating that the original source file hash matches the cryptographic stream hash.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T112522.636.png)

Demonstrating that the original source file hash matches the cryptographic stream hash.

It is important that you ensure your input file’s order matches the order of bytes in the source file in order to get the same hash. Notice that if you change the ordering of the input files with `Sort-Object` then the hash changes completely.

![Changing the order of input files changes the output hash.](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T112547.458.png)

Changing the order of input files changes the output hash.

Armed with this knowledge, you can get that 5TB file hashed in almost no time at all!

## Next Steps

In this article, you’ve learned how to use the `Get-FileHash` cmdlet in PowerShell. You’ve also learned about the nature of hashing, secure hashing requirements, and best practices to help you pick the right algorithm for your needs.

Armed with this knowledge, you’ll be able to thoroughly identify changes to data, even when only a single character is changed!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fget-filehash%2F&text=How%20to%20Use%20the%20Get-FileHash%20PowerShell%20Cmdlet)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fget-filehash%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fget-filehash%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
