PowerShell function: Get-HashFromString

PowerShell function: Get-HashFromString

Be the first to vote!

Hi, everyone!

In a previous post, I built a function on how to get an MD5 hash from a file stored in memory.  I had a particular need this holiday season to compute a hash of a text input string, so I thought I would share my solution with you:

Function Get-HashFromString($String)
{
  $md5hash = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
  $utf = New-Object -TypeName System.Text.UTF8Encoding
  $value = [System.BitConverter]::ToString($md5hash.ComputeHash($utf.GetBytes($String)))
  $value = $value.Replace("-", "")
  Return $value
}

Once this function is loaded (interactively, dot sourced, part of your $PROFILE, whatever makes your world go ’round), you can simply run Get-HashFromString <string value> and off you go! A new hash!

Published by Aaron Guilmette

Helping companies conquer inferior technology since 1997. I spend my time developing and implementing technology solutions so people can spend less time with technology. Specialties: Active Directory and Exchange consulting and deployment, Virtualization, Disaster Recovery, Office 365, datacenter migration/consolidation, cheese.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.