GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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) #12531

New issue
Copy link
New issue
Copy link
Closed
Closed
Get-StringHash (something like Get-FileHash but not file based)#12531
Copy link
Labels
Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issues

Description

@Conan1989
Conan (Conan1989)
opened on Apr 30, 2020
Issue body actions

Summary 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 '-' )
    }
}

Activity

  1. Conan (Conan1989)
    added
    Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bug
    on Apr 30, 2020
  2. scriptingstudio commented on Apr 30, 2020

    @scriptingstudio
    Matthew Gray (scriptingstudio)
    on Apr 30, 2020
    More actions

    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
    
  3. iSazonov commented on Apr 30, 2020

    @iSazonov
    Ilya (iSazonov)
    on Apr 30, 2020
    Collaborator
    More actions

    Conan (@Conan1989) Our workflow is that you should create RFC in PowerShell-RFC repository for discussion and voting.
    But it is already https://github.com/PowerShell/PowerShell-RFC/blob/master/2-Draft-Accepted/RFC0018-Get-StringHash.md

  4. Ilya (iSazonov)
    added
    Area-Cmdlets-Utility
    on Apr 30, 2020
  5. alx9r commented on Apr 30, 2020

    @alx9r
    alx9r
    on Apr 30, 2020
    More actions

    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.

  6. Conan1989 commented on May 14, 2020

    @Conan1989
    Conan (Conan1989)
    on May 14, 2020
    Author
    More actions

    Conan (@Conan1989) Our workflow is that you should create RFC in PowerShell-RFC repository for discussion and voting.
    But it is already https://github.com/PowerShell/PowerShell-RFC/blob/master/2-Draft-Accepted/RFC0018-Get-StringHash.md

    Thanks. Is there any way to try get some movement on that RFC? Seems it's been idle since 2017

    GitHub
    PowerShell/PowerShell-RFC
    RFC (Request for Comments) documents for community feedback on design changes and improvements to PowerShell ecosystem - PowerShell/PowerShell-RFC
  7. iSazonov commented on May 14, 2020

    @iSazonov
    Ilya (iSazonov)
    on May 14, 2020
    Collaborator
    More actions

    Conan (@Conan1989) You can contribute and implement the RFC.

  8. iSazonov commented on May 15, 2020

    @iSazonov
    Ilya (iSazonov)
    on May 15, 2020
    Collaborator
    More actions

    I forget about #3395

  9. lahell commented on Feb 12, 2023

    @lahell
    lahell (lahell)
    on Feb 12, 2023
    More actions

    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.

  10. lahell commented on Feb 12, 2023

    @lahell
    lahell (lahell)
    on Feb 12, 2023
    More actions

    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.

  11. lahell commented on Feb 12, 2023

    @lahell
    lahell (lahell)
    on Feb 12, 2023
    More actions

    I have tested the code from https://github.com/iSazonov/PowerShell/tree/gethashrfc and it looks like it will fail tests in DefaultCommands.Tests.ps1 unless we change the tests or the GetScriptBlock for DisplayName in AliasInfo.

    GitHub
    GitHub - iSazonov/PowerShell at gethashrfc
    PowerShell for every system! Contribute to iSazonov/PowerShell development by creating an account on GitHub.
  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    microsoft-github-policy-serviceBot
    on Nov 16, 2023 – with Microsoft GitHub Policy Service
    Contributor
    More actions

    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.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    microsoft-github-policy-serviceBot
    on Nov 16, 2023 – with Microsoft GitHub Policy Service
    Contributor
    More actions

    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.

  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    microsoft-github-policy-serviceBot
    on Nov 16, 2023 – with Microsoft GitHub Policy Service
    Contributor
    More actions

    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.

  15. microsoft-github-policy-service
    added
    Resolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or more
    on Nov 16, 2023
  16. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    microsoft-github-policy-serviceBot
    on Nov 23, 2023 – with Microsoft GitHub Policy Service
    Contributor
    More actions

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  17. microsoft-github-policy-service
    closed this as completedon Nov 23, 2023
  18. boernsen-development commented on Dec 16, 2025

    @boernsen-development
    boernsen (boernsen-development)
    on Dec 16, 2025
    More actions

    Please reopen this :)

  19. boernsen-development commented on Dec 16, 2025

    @boernsen-development
    boernsen (boernsen-development)
    on Dec 16, 2025
    More actions

    Workaround

    $string = "any string"
    Get-FileHash -InputStream ([IO.MemoryStream]::new([Text.Encoding]::Utf8.GetBytes($string)) -Algorithm SHA256 | Select-Object -ExpandProperty Hash
  20. Ryan Yates (kilasuit)
    added
    WG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issues
    and removed
    WG-Cmdlets-Utility
    on Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.