PSHash.psm1

function Get-StringHash {
    <#
    .Synopsis
    Creates a hash value from an input string.
     
    .Description
    Creates a hash value from an input string.
     
    Authored by Trevor Sullivan, Microsoft MVP.
     
    .Parameter Text
    The string value that will be hashed.
     
    .Parameter Type
    The hashing algorith that will be used to hash the text passed into the -Text parameter.
     
    .Example
    ### Get a string hash using named parameters
    PS > Get-StringHash -Text @('asdf', 'test');
     
    Text Hash
    ---- ----
    asdf 912ec803b2ce49e4a541068d495ab570
    test 098f6bcd4621d373cade4e832627b4f6
     
    .Example
    ### Get a string hash using pipelining
    PS > "string1", "string2" | Get-StringHash;
     
    Text Hash
    ---- ----
    string1 34b577be20fbc15477aadb9a08101ff9
    string2 91c0c59c8f6fc9aa2dc99a89f2fd0ab5
 
    .Link
    https://trevorsullivan.net
    https://artofshell.com
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string[]] $Text
      , [Parameter(Mandatory = $false)]
        [Alias('HashType')]
        [ValidateSet('MD5')]
        [string] $Type = 'MD5'
    )
    begin {
        $MD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
        $UTF8 = New-Object -TypeName System.Text.UTF8Encoding;
    }
    
    process {
        foreach ($Item in $Text) {
            if ($Type -eq 'MD5') {
                [PSCustomObject]@{
                    Text = $Item;
                    Hash = [System.BitConverter]::ToString($MD5.ComputeHash($UTF8.GetBytes($Item))).Replace('-','').ToLower();
                }
            }
        }
    }
    
    end { }
}

New-Alias -Name gsh -Value Get-StringHash -Description 'Invokes the Get-StringHash function in the PSHash PowerShell module.';