http://poshcode.org/2154I simply added a switch for strings to accompany the current file option when calculating hashes. So, this is really Lees function with a little extra cut-and-paste code.
function Get-ObjectHash{################################################################################## Get-ObjectHash#### Adapted From Windows PowerShell Cookbook (OReilly)## by Lee Holmes (http://www.leeholmes.com/guide)################################################################################<#.SYNOPSISGet the hash of an input file or string..EXAMPLEGet-FileHash myFile.txtGets the hash of a specific file.EXAMPLEdir | Get-FileHashGets the hash of files from the pipeline.EXAMPLEGet-FileHash myFile.txt -Hash SHA1Gets the has of myFile.txt, using the SHA1 hashing algorithm.EXAMPLEGet-ObjectHash stringGets the MD5 hash of the string string..EXAMPLEGet-ObjectHash -String @(test,string,MD5) -HashAlgorithm SHA256Gets the SHA256 hash of an array of strings.#>[CmdletBinding(DefaultParameterSetName = String)]param(## The string to check[Parameter(ParameterSetName = String)][String[]]$String,## The path of the file to check[Parameter(ParameterSetName = Path)][String[]]$Path,## The algorithm to use for hash computation[ValidateSet("MD5", "SHA1", "SHA256", "SHA384", "SHA512")]$HashAlgorithm = "MD5")Set-StrictMode -Version Latest## Create the hash object that calculates the hash of our file.$hashType = [Type] "System.Security.Cryptography.$HashAlgorithm"$hasher = $hashType::Create()
class="MsoNormal"> if($PSBoundParameters.ContainsKey(Path))
{
## Create an array to hold the list of files
$files = @()
## If they specified the file name as a parameter, add that to the list
## of files to process
if($path)
{
$files += $path
}
## Otherwise, take the files that they piped in to the script.
## For each input file, put its full name into the file list
else
{
$files += @($input | Foreach-Object { $_.FullName })
}
## Go through each of the items in the list of input files
foreach($file in $files)
{
## Skip the item if it is not a file
if(-not (Test-Path $file -Type Leaf)) { continue }
## Convert it to a fully-qualified path
$filename = (Resolve-Path $file).Path
## Use the ComputeHash method from the hash object to calculate
## the hash
$inputStream = New-Object IO.StreamReader $filename
$hashBytes = $hasher.ComputeHash($inputStream.BaseStream)
$inputStream.Close()
## Convert the result to hexadecimal
$builder = New-Object System.Text.StringBuilder
$hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }
## Return a custom object with the important details from the
## hashing
$output = New-Object PsObject -Property @{
Path