The Kirkland Coder: September 2013

Thursday, September 26, 2013

MD5 Hash function

Had to get the MD5 Hash on some files and needed a light weight script. So here it is:

function Get-Md5Hash
{
    <#
        .SYNOPSIS
        This function returns the Md5 Hash for a given file.
 
        .DESCRIPTION
        This function returns the Md5 Hash for a given file.
        You can Pipe in files or just use one at a time.
 

        .EXAMPLE
        PS C:\> $ReturnedHash = Get-Md5Hash "c:\test\MyFile.txt"
 
        .EXAMPLE
        PS C:\> @("C:\TEST\TestFile1.txt","C:\TEST\TestFile2.txt","C:\TEST\TestFile3.txt") | Get-Md5Hash
 
        .INPUTS
        A file name/location.
 
        .OUTPUTS
        The Md5 Hash of the given file is returned.
 
        .NOTES
        Function Name: Get-Md5Hash
        Original Author: Norman Skinner (normans)
        Original Created on: 09/26/2013
        Version: 1.0.0.0
 
        HISTORY:
        ===========#==============#======================================
        Date       | User         | Description
        -----------+--------------+--------------------------------------
        09/26/2013 | normans      | Created script
        -----------+--------------+--------------------------------------
                   |              | 
        -----------+--------------+--------------------------------------
                   |              | 
        -----------+--------------+--------------------------------------
    #>
 
    param
    (
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [string]
        # This the file for getting the Md5 hash.
        $File
    )
    PROCESS
    {
        $MD5object = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
        $Md5Hash   = [System.BitConverter]::ToString($MD5object.ComputeHash([System.IO.File]::ReadAllBytes($File)))
        return $Md5Hash
    }
}