0

I am developing a PowerShell script for calculating checksum of zip files. I have to execute it in both W7 and W10. I have noticed that certUtil commmand returns strings like A2 5B 8A... in W7, but in W10 it returns the same string but without spaces. So I decided to remove the spaces to uniform it, setting the output to a variable and then remove the spaces... but it does not work.

for /f  "delims=" %%f in ('dir %~dp0*.zip /b') do (
    echo %%~f:
    $result = certUtil -hashfile "%~dp0%%~f" SHA512 | find /i /v "SHA512" | 
        find /i /v "certUtil"
    $result = $result -replace '\s', ''
    echo %result%
    set /a counter += 1
    echo.
)

Do you know how to remove them?

  • This is some weird mesh of Shell and Powershell. This wouldnt work. – ArcSet Oct 11 '18 at 14:43
  • Thanks for your answer! Sorry, I have to use only Shell, I mixed it with another script I am developing :/ . Here Powershell is not allowed. – user3560080 Oct 15 '18 at 06:52

2 Answers2

2

So it seems in your example you are using Shell Commands like For, Echo, Set and then you mixed in powershell commands like $

You should use all powershell since you said you were working on a powershell script.

Get-ChildItem "C:\TEST" -Include *.zip -File -Recurse | %{
    Get-FileHash $_ -Algorithm SHA512 | select Path, Hash
}

This gets all zip files in Test in and then using Get-Filehash we then use the Sha512 algorithm. Return the Path of File and Hash.

This will require atleast Powershell 4.0

ArcSet
  • 6,518
  • 1
  • 20
  • 34
  • Thanks for your answer! Sorry, I have to use only Shell, I mixed it with another script I am developing :/ . Here Powershell is not allowed. – user3560080 Oct 15 '18 at 06:51
0

For a solution that works with the builtin powershell versions on both 7 and 10 (version 2 and 5 respectively), I'd stick with certutil.

The second line of output from certutil -hashfile contains the hash, so grab that like this:

Get-ChildItem -Filter *.zip -Recurse |ForEach-Object {
    # call certutil, grab second line of output (index 1)
    $hashString = @(certutil -hashfile """$($_.FullName)""" SHA512)[1]
    # remove any non-word characters from the output:
    [regex]::Replace($hashString,'[\W]','')
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks for your answer! Sorry, I have to use only Shell, I mixed it with another script I am developing :/ . Here Powershell is not allowed. – user3560080 Oct 15 '18 at 06:52