2

When I run the Get-Filehash from powershell it works however:

When I run it from a batch file, it drops last 4 digits.

powershell.exe -NoProfile -ExecutionPolicy -Command "Get-FileHash filename.edi -Algorithm SHA256 | Out-File c:\app\testfileout.txt"
Algorithm       Hash                                                           
---------       ----                                                           
SHA256          3EFEC59BFB0573061C5CD2F72A684663B60CA3D0D91C67CBDBBE366A59FE...

How do I get the output file to ONLY be:

3EFEC59BFB0573061C5CD2F72A684663B60CA3D0D91C67CBDBBE366A59FE4A8F

Which gives me the full hash inside Powershell when I run this:

Get-FileHash filename.edi -Algorithm SHA256 | Out-File c:\app\testfileout.txt

I need just the hash without the headers as well as the final 4 digits.

Does anyone have a script that already does this?

1
  • 2
    cmd.exe, despite appearances, is not DOS. Commented Jun 1, 2017 at 21:33

2 Answers 2

2

It returns an object so all you need to do is use Select-Object with ExpandProperty:

powershell.exe -NoProfile -ExecutionPolicy -Command "Get-FileHash filename.edi -Algorithm SHA256 | Select-Object -ExpandProperty Hash | Out-File c:\app\testfileout.txt"

or reference the object property by dot notation:

powershell.exe -NoProfile -ExecutionPolicy -Command "(Get-FileHash filename.edi -Algorithm SHA256).Hash | Out-File c:\app\testfileout.txt"
Sign up to request clarification or add additional context in comments.

Comments

1

With pure , you can do it like this:

cmd /V /C for /F "tokens=*" %H in ('certutil -hashfile "filename.edi" SHA256 ^^^| find /V ":"') do @(set "HASH=%H" ^& echo !HASH: =!) > "C:\app\testfileout.txt"

In a , it may look like this:

setlocal EnableDelayedExpansion
for /F "tokens=*" %%H in ('
    certutil -hashfile "filename.edi" SHA256 ^| find /V ":"
') do @(
    set "HASH=%%H" & echo !HASH: =!
) > "C:\app\testfileout.txt"
endlocal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.