Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PowerShell v2 port of the Get-FileHash function. This version of Get-Hash supports hashing files and strings.
function Get-Hash
Get-Hash is a PowerShell Version 2 port of Get-FileHash that supports hashing files, as well as, strings.
.PARAMETER InputObject
This is the actual item used to calculate the hash. This value will support [Byte[]] or [System.IO.Stream] objects.
Specifies the path to a file to hash. Wildcard characters are permitted.
A string to calculate a cryptographic hash for.
Specified the character encoding to use for the string passed to the Text parameter. The default encoding type is Unicode. The acceptable values for this parameter are:
- BigEndianUnicode
- Default
- Unicode
- UTF32
- UTF7
- UTF8
.PARAMETER Algorithm
Specifies the cryptographic hash function to use for computing the hash value of the contents of the specified file. A cryptographic hash function includes the property that it is not possible to find two distinct inputs that generate the same hash values. Hash functions are commonly used with digital signatures and for data integrity. The acceptable values for this parameter are:
- SHA1
- SHA256
- SHA384
- SHA512
- MACTripleDES
- MD5
If no value is specified, or if the parameter is omitted, the default value is SHA256.
For security reasons, MD5 and SHA1, which are no longer considered secure, should only be used for simple change validation, and should not be used to generate hash values for files that require protection from attack or tampering.
This function was adapted from
Author: Jared Atkinson (@jaredcatkinson)
License: BSD 3-Clause
Required Dependencies: None
Optional Dependencies: None
Get-Hash -Text 'This is a string'
Get-Hash -FilePath C:\This\is\a\filepath.exe
[Parameter(Mandatory = $true, ParameterSetName = 'Object')]
[Parameter(Mandatory = $true, ParameterSetName = 'File')]
[Parameter(Mandatory = $true, ParameterSetName = 'Text')]
[Parameter(ParameterSetName = 'Text')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF7', 'UTF8')]
$Encoding = 'Unicode',
[ValidateSet("MACTripleDES", "MD5", "RIPEMD160", "SHA1", "SHA256", "SHA384", "SHA512")]
$Algorithm = "SHA256"
$FullPath = Resolve-Path -Path $FilePath -ErrorAction Stop
$InputObject = [System.IO.File]::OpenRead($FilePath)
Get-Hash -InputObject $InputObject -Algorithm $Algorithm
$retVal = New-Object -TypeName psobject -Property @{
Algorithm = $Algorithm.ToUpperInvariant()
Hash = $null
$InputObject = [System.Text.Encoding]::$Encoding.GetBytes($Text)
Get-Hash -InputObject $InputObject -Algorithm $Algorithm
if($InputObject.GetType() -eq [Byte[]] -or $InputObject.GetType().BaseType -eq [System.IO.Stream])
# Construct the strongly-typed crypto object
$hasher = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
# Compute file-hash using the crypto object
[Byte[]] $computedHash = $Hasher.ComputeHash($InputObject)
[string] $hash = [BitConverter]::ToString($computedHash) -replace '-',''
$retVal = New-Object -TypeName psobject -Property @{
Algorithm = $Algorithm.ToUpperInvariant()
Hash = $hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.