You need to break it down as you have so many syntax errors. Do each part by itself first. Starting with
Get-FileHash $FileName
Does this produce output? Ok great, what about
(Get-FileHash $FileName).Hash
OK cool, now let’s add more
(Get-FileHash $FileName).Hash -Algorithm SHA-256
Holy moly, that’s not good. When you added ( ).Hash
you completed the Get-FileHash
command and output only the value of the Hash
property. Effectively the command is
SomeLongHash -Algorithm SHA-256
Which is not valid powershell and clearly not what you’re attempting. So assuming you want to add -Algorithm
to Get-FileHash
, it must be inside the parenthesis
(Get-FileHash $FileName -Algorithm SHA-256).Hash
OK now there’s an error with -Algorithm
argument. This is where I’m guessing you got this code from AI. If you simply type Get-FileHash $FileName -Algorithm
and press tab, it will complete all the options available. This will reveal the value you are looking for is SHA256
OK so we fixed that
(Get-FileHash $FileName -Algorithm SHA256).Hash
This produces a SHA256 hash. OK let’s add more
(Get-FileHash $FileName -Algorithm SHA256).Hash -eq $Hash
Does that produce a true/false like you expect? OK add your last part
(Get-FileHash $FileName -Algorithm SHA256).Hash -eq $Hash Write-Output
OK now we have a syntax error again. I’m not sure what you’re trying to achieve by adding that, as the output will already output as you’ve seen. So writing out the output that’s already being output is unnecessary. I’d just leave it off.
I would recommend simplifying until you get a better understanding of powershell.
$filehash = Get-FileHash -Path $FileName -Algorithm SHA256
if($filehash){
$filehash.hash -eq $Hash
}
else{
Write-Host "No filehash was produced for $FileName" -ForeGroundColor Yellow
}