Solved

md5sum check using powershell v 1.0

Posted on 2016-10-01
4
118 Views
Last Modified: 2016-10-01
Hi experts,
could you advise how to check md5sum check on a file using Microsoft powershell v1.0

Thanks in advance
0
Comment
Question by:enthuguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Learnctx earned 500 total points
ID: 41825055
This is the function Microsoft created with later versions of PowerShell (the Get-FileHash function). I've modified it to work with PowerShell v1.

Function Get-FileHash
{
	[CmdletBinding(DefaultParameterSetName = "Path")]
	param(
		[System.String[]]
		$Path,

		[Alias("PSPath")]
		[System.String[]]
		$LiteralPath,

		[System.String]
		$Algorithm="SHA256"
	)
	
	# Construct the strongly-typed crypto object
	$hasher = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
	
	$pathsToProcess = @()
	
	if($PSCmdlet.ParameterSetName  -eq "LiteralPath")
	{
		$pathsToProcess += Resolve-Path -LiteralPath $LiteralPath
	}
	else
	{
		$pathsToProcess += Resolve-Path $Path
	}
	
	foreach($filePath in $pathsToProcess)
	{
		if(Test-Path -LiteralPath $filePath -PathType Container)
		{
			continue
		}
		
		try
		{
			# Read the file specified in $FilePath as a Byte array
			[system.io.stream]$stream = [system.io.file]::OpenRead($FilePath)

			# Compute file-hash using the crypto object
			[Byte[]] $computedHash = $hasher.ComputeHash($stream)
		}
		catch [Exception]
		{
			$errorMessage = [Microsoft.PowerShell.Commands.UtilityResources]::FileReadError -f $FilePath, $_
			Write-Error -Message $errorMessage -Category ReadError -ErrorId "FileReadError" -TargetObject $FilePath
			return
		}
		finally
		{
			if($stream)
			{
				$stream.Close()
			}
		}
		
		# Convert to hex-encoded string
		[string] $hash = [BitConverter]::ToString($computedHash) -replace '-',''

		$retVal = [PSCustomObject] @{
			Algorithm = $Algorithm.ToUpperInvariant()
			Hash = $hash
			Path = $filePath
		}
		$retVal.psobject.TypeNames.Insert(0, "Microsoft.Powershell.Utility.FileHash")

		$retVal
	}
}

Open in new window


Calling it is simple:

Get-FileHash -Path "C:\Temp\ExampleFile.txt" -Algorithm MD5

Open in new window


It will do the following file hashes.

SHA1
SHA256
SHA384
SHA512
MACTripleDES
MD5
RIPEMD160
1
 
LVL 95

Expert Comment

by:John Hurst
ID: 41825058
DigestIt 2004 works for MD5 and SHA1 on everything up to Windows 10. You do not really to code something unless you wish to.

http://www.colonywest.us/digestit/
1
 

Author Closing Comment

by:enthuguy
ID: 41825062
Thanks very much, that perfectly worked for me

could you suggest me how to check md5 value on two files. (between left file and right file) and get true or false please?

Is there  a function which could help me

thanks again
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
how to add IIS SMTP to handle application/Scanner relays into office 365.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question