GeezerVison is showing github.com — Get-StringHash (something like Get-FileHash but not file based) · Issue #12531 · PowerShell/PowerShell
Get-StringHash (something like Get-FileHash but not file based) #12531DescriptionSummary of the new feature/enhancement
Hi,
Request a native cmdlet for getting a hash value (MD5, SHA256, etc) of an inputted string.
eg - something like Get-FileHash ... but not file based
justification. I've seen requirements for such a thing around the place, but with incorrectly implemented (hopefully the below isn't too far off) and sub optimal solutions. eg bad code from SE and write to disk, hash from disk with Get-FileHash
Proposed technical implementation details (optional)
something like the following maybe
function Get-StringHash {
[CmdletBinding()]
[OutputType([System.String])]
param (
[ValidateScript({ ![System.String]::IsNullOrEmpty($PSItem) })][System.String]$inputString,
[ValidateSet('MD5', 'SHA1', 'SHA256', 'SHA384', 'SHA512')][System.String]$hashAlgo
)
process {
$ErrorActionPreference = 'stop'
Set-StrictMode -Version 'latest'
$inputBytes = [System.Text.Encoding]::UTF8.GetBytes($inputString)
$hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::Create($hashAlgo)
# [System.Security.Cryptography.HashAlgorithmName]
return ( [System.BitConverter]::ToString( $hashAlgorithm.ComputeHash($inputBytes) ) -replace '-' )
}
}
Activityscriptingstudio commented on Apr 30, 2020 Provide a default value or validation for $hashAlgo, otherwise you'll get
Get-StringHash : You cannot call a method on a null-valued expression.
At line:1 char:1
+ Get-StringHash $s
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-StringHash], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull,Get-StringHash
iSazonov commented on Apr 30, 2020 alx9r commented on Apr 30, 2020 I have an implementation of Get-StringHash that is used by many of our internal powershell modules. For a built-in implementation to be useful in our organization it would need to implement the whole matrix of a significant number of common input (strings, byte arrays, etc), algorithms (CRC, MD5, SHA256, etc) and output formats (hex, base64, ascii, etc).
For most of our use cases we need the property ($s1 | Get-StringHash) -eq ( $s2 | Get-StringHash) to hold for all cases where $s1 and $s2 are the "same". I suspect that is the property needed by many users looking to convert a string to a hash. I have found a significant number of edge cases related to text encoding, EOLs, etc on both the input and output side that prevent that property from holding across PowerShell, .NET, and OS versions and editions. Our organization tests for that property across the subset of combinations for which we need the property to hold and make changes to our implementation to correct for edge cases. Doing that for all possible conditions seems like a substantial undertaking.
I don't think the sort of testing to make that property hold is occurring for other PowerShell commands currently; there are combinations of PowerShell versions where Set-Content and Get-Content roundtrips aren't isomorphic, for example. So if you write a string to a file with PowerShell 5 and read it with PowerShell 7 you can end up with significant observable differences some of which break equality. It's not obvious that there is a conversion that completely corrects for the differences. Get-StringHash would add another layer to that problem. Conan1989 commented on May 14, 2020 iSazonov commented on May 14, 2020 iSazonov commented on May 15, 2020 lahell commented on Feb 12, 2023 What would be the expected result if Get-FileHash becomes an alias for Get-Hash and someone is already using 'C:\file.txt' | Get-FileHash or (Get-ChildItem).FullName | Get-FileHash in their script? Should they get FileHashInfo or StringHashInfo as output? I as a user would expect to get FileHashInfo but if I'm reading the RFC correctly the output would be StringHashInfo which could break many existing scripts. lahell commented on Feb 12, 2023 It will not be obvious to users that Get-FileHash is an alias because it will be the only one with a name like Verb-Noun.
Adding a new cmdlet named Get-StringHash will not break existing scripts, will probably not be as complicated to implement and it will not cause confusion. lahell commented on Feb 12, 2023 microsoft-github-policy-service commented on Nov 16, 2023 This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023 This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023 This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 23, 2023 This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.
boernsen-development commented on Dec 16, 2025 boernsen-development commented on Dec 16, 2025 Workaround
$string = "any string"
Get-FileHash -InputStream ([IO.MemoryStream]::new([Text.Encoding]::Utf8.GetBytes($string)) -Algorithm SHA256 | Select-Object -ExpandProperty Hash
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|