1

I am working on Windows 8.1 Pro with PowerShell 5.0.

Let's say I want to get a pure SHA-256 hash string of a file.

I defined a following function:

function sha256sum([string]$arg) {
    Get-FileHash -Algorithm SHA256 $arg | Format-List | grep Hash
}

Example output:

PS C:\Users\Vlastimil\Downloads> sha256sum .\adr.exe
STDIN
Hash      : B3729AFDB438A1B264D75003379DC8ADD2DADDF91D01674F2844DB114D22D71B

It's almost working as to my wishes, might I ask:

  1. How do I get rid of the first line saying STDIN?

  2. How do I extract only the Hash string from the second line?

  3. How do I convert the string to lower-case?

2

You can get the STDIN out by using the following function:

function sha256sum([string]$arg) {
    Get-FileHash -Algorithm SHA256 $arg | Format-List Hash
}

If you want to get rid of the extra empty rows it produces, use the following function:

function sha256sum([string]$arg) {
    (Get-FileHash -Algorithm SHA256 $arg | Format-List Hash | Out-String).Trim()
}

To get the final result, you will want to use the following function though:

function sha256sum([string]$arg) {
    $Hash = Get-FileHash -Algorithm SHA256 $arg | Select -ExpandProperty Hash
    $Hash.ToLower()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.