Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I check the filehash of a file thats online in PowerShell?

So well, I am making a pull request to Chris Titus Tech’s Ultimate Windows Toolkit, and I wanna make something that checks if it’s updated. But when I try running Get-FileHash -Algorithm SHA256 https://raw.githubusercontent.com/fgclue/etcher/master/desktop.ini It just says:

Get-FileHash: Cannot find drive. A drive with the name 'https' does not exist.

And I want to make it look something like this:

$hash = Get-FileHash -Algorithm SHA256 URL
$chash = Get-FileHash -Algorithm SHA256 win10debloat.ps1

if ($hash -ne $chash){
     Write-Host "There is an update!"
     Write-Host "Update?"
     $Opt = Read-Host "Choice"     
     if (Opt -ne y){
          exit
     }
     if (Opt -ne yn){
          Start-Process "https://github.com/ChrisTitusTech/win10script/"
          Write-Host Please download the new version and close this windows.
     }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’m not totally sure what actually you want to compare but here is how you can test if what you have is up to date without downloading anything, in this case I believe you need to use IO.MemoryStream to get the hash of the remote file.

There is likely a better way to do it, also note, this code is using UTF8 Encoding with a BOM for getting the remote file bytes, you need to make sure the encoding is correct, i.e.: if you need UTF8 no BOM, use [System.Text.UTF8Encoding]::new().GetBytes(...) instead.

$uri = 'https://raw.githubusercontent.com/fgclue/etcher/master/desktop.ini'
$theHashIHave = Get-FileHash myfilehere.ext -Algorithm SHA256

try {
    $content = Invoke-RestMethod $uri
    $memstream = [System.IO.MemoryStream]::new(
        [System.Text.Encoding]::UTF8.GetBytes($content)
    )
    $thisFileHash = Get-FileHash -InputStream $memstream -Algorithm SHA256
    if($theHashIhave.Hash -eq $thisFileHash.Hash) {
        "all good"
    }
    else {
        "should update here"
    }
}
finally {
    $memstream.foreach('Dispose')
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading