Hashing in powershell, output to a text file.


  1. Posts : 582
    Windows 10 Pro 64 bit 19044.1706
       #1

    Hashing in powershell, output to a text file.


    I want to hash file with sha512 and output it to txt file, as it doesn't show in powershell, because it is too long!

    So code is like this.
    Code:
    get-filehash "D:\files\OS\Linux\Debian\debian-10.5.0-amd64-netinst.iso" -Algorithm SHA512 c:\test.txt
    It gives error code like this:
    Code:
    Get-FileHash : A positional parameter cannot be found that accepts argument 'c:\test.txt'.
    At line:1 char:1
    + get-filehash -LiteralPath "D:\files\OS\Linux\Debian\debian-10.5.0-amd ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-FileHash], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Get-FileHash
    I found only some over complicated code i can't adjust to my needs, or to do something else. However how i did this, it should work. I have no idea why it doesn't... I started it as admin.
      My Computer


  2. Posts : 14,215
    Win10 Pro and Home, Win11 Pro and Home, Win7, Linux Mint
       #2

    I can't speak to the hashing but being curious I read your post. I see an issue with the part about 'c:\test.txt' in that one usually can't save to the root of the boot/OS drive, need to use a folder that exists or you create on the drive.
      My Computers


  3. Posts : 582
    Windows 10 Pro 64 bit 19044.1706
    Thread Starter
       #3

    Berton said:
    I can't speak to the hashing but being curious I read your post. I see an issue with the part about 'c:\test.txt' in that one usually can't save to the root of the boot/OS drive, need to use a folder that exists or you create on the drive.
    I tried d and same result. Cmd/powershell can write to c as far as i know. I used it for tracert for example. I can't believe why it is so stupid, that it can't show rest of hash on the next line, instead there are ...
      My Computer


  4. Posts : 1,728
    Windows 10 Pro x64 22H2
       #4

    Problem is that only -Path can be positional but you specified 2 paths, to output result to file below command will work:

    Code:
    Get-FileHash -Path "D:\files\OS\Linux\Debian\debian-10.5.0-amd64-netinst.iso" -Algorithm SHA512 | Out-File c:\test.txt
    - - - Updated - - -

    If you want to compare hashes using PowerShell (ex. no manual comparison which is error prone) then do this:

    Save file hash into variable:
    Code:
    $ActuallHash = Get-FileHash -Path "D:\files\OS\Linux\Debian\debian-10.5.0-amd64-netinst.iso" -Algorithm SHA512 | Select-Object -ExpandProperty Hash

    Save expected hash into another variable: (ex. from website where you downloaded file in question)
    Code:
    $ExpectedHash = "B4D5AB9F66ED0BB9D5C2B13FE385A20810EFEB875862A3560D35BA5FC447E9705B0AE2128FDE1414851FDC0B54D820405BF5E0F4C78CAB26B4C45F7F92F5EBC2"

    Finally compare hashes:
    Code:
    $ActuallHash -eq $ExpectedHash
    If the output is "True" the verification was success, if "False" there is hash mismatch and file in question may be corrupt or modified by somebody.
      My Computer


  5. Posts : 989
    Microsoft Windows 10 Home
       #5

    zebal said:
    Code:
    Get-FileHash -Path "D:\files\OS\Linux\Debian\debian-10.5.0-amd64-netinst.iso" -Algorithm SHA512 | Out-File c:\test.txt
    Out-File just redirects the formatted console output to a text file, so it;s still truncated:

    Code:
    Algorithm       Hash                                                                   Pa
                                                                                           th
    ---------       ----                                                                   --
    SHA512          53F19BD48FED43CCE9BCC907A5718FB425DCAB44055173B160FA692F8CA329BF48C... C:
    If you really wanted the output in a file, Export-Csv would be the logical choice, as you could retrieve the entire output object iwith Import-Csv for further processing.
    Code:
    Get-FileHash ProcMonLog.log.PML -Algorithm SHA512 | Export-csv "$HOME\out3.txt" -NoTypeInformation
    If you were primarily interested in viewing the un-truncated hash, Out-GridView would be the best choice:
    Code:
    Get-FileHash ProcMonLog.log.PML -Algorithm SHA512 | Out-GridView
    Hashing in powershell, output to a text file.-screenshot-827-.png
      My Computer


  6. Posts : 1,728
    Windows 10 Pro x64 22H2
       #6

    Good point made by @KeithM, I didn't notice truncated output but one another workaround is to use Format-List

    Code:
    Get-FileHash -Path "D:\files\OS\Linux\Debian\debian-10.5.0-amd64-netinst.iso" -Algorithm SHA512 | Format-List | Out-File c:\test.txt
    However the main issue is retrieving only the "Hash" from file once it has been saved to file, in which case either ConvertFrom-Csv or ConvertFrom-Json would do the trick.

    For example using JSON file format:

    Code:
    ConvertTo-Json -InputObject (Get-FileHash -Path "D:\files\OS\Linux\Debian\debian-10.5.0-amd64-netinst.iso" -Algorithm SHA512) | Out-File c:\test.json
    And then to read only the Hash out of a file:
    Code:
    Get-Content -Path c:\test.json | ConvertFrom-Json | Select-Object -ExpandProperty Hash
      My Computer


  7. Posts : 17,049
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #7

    Surely the most appropriate method would be to set the hash to a variable that is then compared to the expected hash by the PS script leaving the human to read only the results [they either match or do not match].

    Guidance on this approach is given in Working with file hashes in PowerShell - TechGenix

    Personally, if I thought the game was worth the candle, I'd set this up using a 3 variable input box along the lines of Powershell Custom GUI input box for passing values to Variables - Vikas Sukhija
    Hashing in powershell, output to a text file.-hash-variable-input-block.png
    Sample code block enclosed Hash variable input block.ps1

    Denis
      My Computer


  8. Posts : 582
    Windows 10 Pro 64 bit 19044.1706
    Thread Starter
       #8

    KeithM said:
    Out-File just redirects the formatted console output to a text file, so it;s still truncated:

    Code:
    Algorithm       Hash                                                                   Pa
                                                                                           th
    ---------       ----                                                                   --
    SHA512          53F19BD48FED43CCE9BCC907A5718FB425DCAB44055173B160FA692F8CA329BF48C... C:
    If you really wanted the output in a file, Export-Csv would be the logical choice, as you could retrieve the entire output object iwith Import-Csv for further processing.
    Code:
    Get-FileHash ProcMonLog.log.PML -Algorithm SHA512 | Export-csv "$HOME\out3.txt" -NoTypeInformation
    If you were primarily interested in viewing the un-truncated hash, Out-GridView would be the best choice:
    Code:
    Get-FileHash ProcMonLog.log.PML -Algorithm SHA512 | Out-GridView
    Hashing in powershell, output to a text file.-screenshot-827-.png
    Thank you very much, this worked!!!
      My Computer


  9. Posts : 1
    Windows
       #9

    Hash to .txt


    I found this wrote straight to a text file (albeit in a not so clean format as Out-File) :)

    Code:
    Get-FileHash ffxivboot.exe -Algorithm SHA1 | Export-csv "ffxivboot-hash.txt"

    Hashing in powershell, output to a text file.-screenshot-2024-07-03-135943.png
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 08:00.
Find Us




Windows 10 Forums