Thursday, September 28, 2023
HomePowershellLet's Study the Get-FileHash Command

Let’s Study the Get-FileHash Command

Somebody, someplace, despatched me down a path. On the finish of it, whereas it isn’t the place I wanted to be, I discovered — or relearned fairly — concerning the Get-FileHash cmdlet. Whether or not about it or not, we’ll shortly cowl it and stroll by way of some examples, as properly. Get-FileHash, and I quote, “Computes the hash worth for a file through the use of a specified hash algorithm.” That is what it does, however just isn’t the why. Right here is its reference web page, nevertheless:, and the why, is certainly in there; you must learn it.

The thought, for these that aren’t going to learn it, is that we are able to acquire a file’s hash after which verify the hash to make sure the file has not been modified. At this level in your profession, you’ve got doubtless seen file hashes close to, or alongside, a file obtain. Checking the file hash in opposition to the file, after it’s downloaded, means that you can make sure that the file is the appropriate one and that it was not altered by the obtain course of, or the rest. It’s what you had been anticipating.

If you will run any of my beneath instructions, first make sure your working listing and that it’s a location the place you’ve got permissions to write down. We are going to begin by creating a brand new file, including a sentence to it, after which returning that content material to make sure it’s correctly in place.

New-Merchandise -Identify hashfile.txt -ItemType File

Mode                 LastWriteTime         Size Identify
----                 -------------         ------ ----
-a---           4/19/2022  7:06 PM              0 hashfile.txt

Add-Content material -Path .hashfile.txt -Worth 'That is our file initially.'
Get-Content material -Path .hashfile.txt
That is our file initially.

That each one works and so now we now have a file with which may experiment. In case you are questioning the way you be taught PowerShell, that is the way you do it. Observe alongside, as there’s a goodie additional down beneath. On this instance we invoke Get-FileHash in opposition to our file, solely returning the algorithm used and the hash. We’re utilizing Format-Checklist with a view to higher show this content material.

Get-FileHash -Path .hashfile.txt | Tee-Object -Variable SaveMeForLater | Format-Checklist -Property Algorithm,Hash

Algorithm : SHA256
Hash      : 3C55E3C7D4C2EEF6910CB70FC425549981528CBBC0400A705104DC09A9391356

On this instance, we do the identical as we did above, nevertheless, now we’re going to check out the opposite parameter values that the Algorithm parameter will settle for. By default it makes use of SHA256, however it should settle for SHA1, SHA384, SHA512, and MD5, too.

Get-FileHash -Algorithm SHA1 -Path .hashfile.txt | Format-Checklist -Property Algorithm,Hash

Algorithm : SHA1
Hash      : BD002AAE71BEEBB69503871F2AD3793BA5764097

Get-FileHash -Algorithm SHA256 -Path .hashfile.txt | Format-Checklist -Property Algorithm,Hash

Algorithm : SHA256
Hash      : 3C55E3C7D4C2EEF6910CB70FC425549981528CBBC0400A705104DC09A9391356

Get-FileHash -Algorithm SHA384 -Path .hashfile.txt | Format-Checklist -Property Algorithm,Hash

Algorithm : SHA384
Hash      : E6BC50D6465FE3ECD7C7870D8A510DC8071C7D1E1C0BB069132ED712857082E34801B20F462E4386A6108192C076168A

Get-FileHash -Algorithm SHA512 -Path .hashfile.txt | Format-Checklist -Property Algorithm,Hash

Algorithm : SHA512
Hash      : C0124A846506B57CE858529968B04D2562F724672D8B9E2286494DB3BBB098978D3DA0A9A1F9F7FF0D3B862F6BD1EB86D301D025B80C0FC97D5B9619A1BD7D86

Get-FileHash -Algorithm MD5 -Path .hashfile.txt | Format-Checklist -Property Algorithm,Hash

Algorithm : MD5
Hash      : 30091603F57FE5C35A12CB43BB32B5F5

For enjoyable, let’s loop by way of these values and pump out all of the hashes, one proper after one other. Discover that we’re utilizing hard-coded values for the Algorithm parameter. Obnoxious. We are going to get to a different method, which is/was the goodie I discussed above. The extra I give it some thought although — as I’ve been within the final 10 minutes — the extra I feel it would want its personal put up. Anyway, extra on that quickly.

    | ForEach-Object  Choose-Object -Property Algorithm,Hash

Algorithm Hash
--------- ----
SHA1      BD002AAE71BEEBB69503871F2AD3793BA5764097
SHA256    3C55E3C7D4C2EEF6910CB70FC425549981528CBBC0400A705104DC09A9391356
SHA384    E6BC50D6465FE3ECD7C7870D8A510DC8071C7D1E1C0BB069132ED712857082E34801B20F462E4386A6108192C076168A
SHA512    C0124A846506B57CE858529968B04D2562F724672D8B9E2286494DB3BBB098978D3DA0A9A1F9F7FF0D3B862F6BD1EB86D301D025B80C0FC97D5B9619A1BD7D86
MD5       30091603F57FE5C35A12CB43BB32B5F5

And, there they’re once more. The varied hashes for our file. Now, let’s add some new recordsdata. All we’re going to do is copy and paste our hashfile.txt to the identical listing two instances. Rename them in order that along with hashfile.txt, you’ve got hashfilecopy.txt and hashfile.copy. Watch these names and file extensions, though actually, how essential do you need to be? Give it some thought…

When checking the hash of a file, we confirm the file contents haven’t modified. They usually haven’t been modified! Solely the file title and file extension have. You might be beginning to see how this generally is a useful gizmo and guess what? It’s built-in.

Get-ChildItem | Get-FileHash | Format-Checklist

Algorithm : SHA256
Hash      : 3C55E3C7D4C2EEF6910CB70FC425549981528CBBC0400A705104DC09A9391356
Path      : C:UserstommymaynardDocumentsPowerShell_Get-FileHashhashfile.copy

Algorithm : SHA256
Hash      : 3C55E3C7D4C2EEF6910CB70FC425549981528CBBC0400A705104DC09A9391356
Path      : C:UserstommymaynardDocumentsPowerShell_Get-FileHashhashfile.txt

Algorithm : SHA256
Hash      : 3C55E3C7D4C2EEF6910CB70FC425549981528CBBC0400A705104DC09A9391356
Path      : C:UserstommymaynardDocumentsPowerShell_Get-FileHashhashfilecopy.txt

Now actual fast, let’s make one other change. I’m going to repeat and paste hashfile.txt one final time. This copy I’ve renamed to hashfilechanged.txt. I opened it up and added a second sentence to it. Beneath the primary line, I wrote, “That is our file on the finish.”

Get-Content material -Path .hashfilechanged.txt

That is our file initially.
That is our file on the finish.

Get-FileHash -Path .hashfilechanged.txt | Tee-Object -Variable SaveMeForNow | Format-Checklist -Property Algorithm,Hash

Algorithm : SHA256
Hash      : 6998575555A0B7086E43376597BBB52582A4B9352AD4D3D642F38C6E612FDA76

I used Tee-Object a few instances on this put up to seize the unique hash and this one, after including a second sentence. As you may see, the file contents are certainly totally different now, although the recordsdata might have had the identical title, had been they in numerous directories.



And, the goodie I discussed. It’s official; it should get its personal put up. Why not? I make the principles. I’ll hyperlink it from right here as soon as it’s up and printed!



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments