Author Posts

September 30, 2015 at 8:25 am

I'm creating a file and to protect the integrity of it I would want to attach the MD5 checksum in the header.

Using the below script from I can get the MD5 checksum...But I'm just thinking that as soon as i generate it and then apply it to the header, the MD5 checksum has changed again...Is there a method to adding the checksum in the header without changing it by adding it to the header?

It would in theory look like...

Performed by: $username
Date: $date
MD5: $md5hash
everything else in the report below

  Get-Hash "hello_world.txt"
    Gets the SHA1 from myFile.txt file. When there's no explicit parameter, it uses -File

    Get-Hash -File "C:\temp\hello_world.txt"
    Gets the SHA1 from myFile.txt file
    C:\PS> Get-Hash -Algorithm "MD5" -Text "Hello Wold!" 
    Gets the MD5 from a string
    C:\PS> "Hello Wold!" | Get-Hash
    We can pass a string throught the pipeline  
    Get-Content "c:\temp\hello_world.txt" | Get-Hash
    It gets the string from Get-Content

    Get-ChildItem "C:\temp\*.txt" | %{ Write-Output "File: $($_)   has this hash: $(Get-Hash $_)" }
    This is a more complex example gets the hash of all "*.tmp" files
    DBA daily stuff ( by Josep Martínez Vilà
    Licensed under a Creative Commons Attribution 3.0 Unported License
    Original post:
function Get-Hash
        [parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="set1")]
        [parameter(Position=0, Mandatory=$true, ValueFromPipeline=$false, ParameterSetName="set2")]
        $file = "",
        [parameter(Mandatory=$false, ValueFromPipeline=$false)]
        [ValidateSet("MD5", "SHA", "SHA1", "SHA-256", "SHA-384", "SHA-512")]
        $algorithm = "SHA1"

        $hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::Create($algorithm)
        $md5StringBuilder = New-Object System.Text.StringBuilder 50
        $ue = New-Object System.Text.UTF8Encoding 

        if ($file){
            try {
                if (!(Test-Path -literalpath $file)){
                    throw "Test-Path returned false."
            catch {
                throw "Get-Hash - File not found or without permisions: [$file]. $_"

            try {        
                [System.IO.FileStream]$fileStream = [System.IO.File]::Open($file, [System.IO.FileMode]::Open);
                $hashAlgorithm.ComputeHash($fileStream) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) }
            catch {
                throw "Get-Hash - Error reading or hashing the file: [$file]"
            finally {
        else {
            $hashAlgorithm.ComputeHash($ue.GetBytes($text)) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) }
        return $md5StringBuilder.ToString()

September 30, 2015 at 8:54 am

Nope. As you say, adding any content to the file immediately changes the hash. Hashes are typically downloaded or sent out in a separate file.

September 30, 2015 at 8:59 am

Ok, that's what I figured but just wanted to ask, just incase. Thanks!