Solved

md5sum check using powershell v 1.0

Posted on 2016-10-01
4
63 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
  • 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 92

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
 

Author Comment

by:enthuguy
ID: 41825066
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now