Powershell: Get-FileHash Terminates at Corrupt File

Created on 23 Feb 2020  路  6Comments  路  Source: PowerShell/PowerShell

When running a script to obtain the hashes of all files in a given directory, PS 7.0.0-rc.3 will terminate the command when it comes across a corrupt file. In PS 5.1, the same command will throw an error but continue generating hashes of additional files. The script I;m using to test is

get-childitem h:\ -Recurse -Force -File | Get-FileHash

In either version of Powershell, I get the error below. The only difference is that the script continues processing in 5.1 and terminates in 7.

Get-FileHash: The file or directory is corrupted and unreadable. : 'H:\Intel\gp\profile.dat'

Area-Cmdlets-Utility Issue-Question Resolution-Fixed

Most helpful comment

  • In WinPS, Get-FileHash is a _function_ that caught _any_ exception in its process block and reported it as a _non-terminating_ error.

  • For PS Core, Get-FileHash was re-implemented as a _cmdlet_ (by @iSazonov, I believe :), and it currently catches only two specific exceptions, the one at hand _not_ being among them, which explains the effectively statement-terminating error (uncaught exception).

https://github.com/PowerShell/PowerShell/blob/697dc5b37149d0dd98c34c33a87c833a23fe467e/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs#L127-L163

Sounds like all that is needed is to add a catch-all catch branch.

All 6 comments

It is "by-design" for cmdlets processing single file to write a terminating error.

@jasonmull, it sounds like your $ErrorPreference might be set differently between 5.1 and 7?

You might try deliberately specifying the error action of 'continue' with the Get-FileHash cmdlet.

I don't think there is supposed to be any difference between 5.1 and 7 as long as all preferences are set the same.

It is "by-design" for cmdlets processing single file to write a terminating error.

This command is not processing a single file. It is running against an entire directory. In 7, it terminates on this file, but in 5.1, it errors and continues.

@jasonmull, it sounds like your $ErrorPreference might be set differently between 5.1 and 7?

You might try deliberately specifying the error action of 'continue' with the Get-FileHash cmdlet.

I don't think there is supposed to be any difference between 5.1 and 7 as long as all preferences are set the same.

I've verified that $ErrorActionPreference is set to Continue in both instances and I've manually defined that action in the script. No change.

  • In WinPS, Get-FileHash is a _function_ that caught _any_ exception in its process block and reported it as a _non-terminating_ error.

  • For PS Core, Get-FileHash was re-implemented as a _cmdlet_ (by @iSazonov, I believe :), and it currently catches only two specific exceptions, the one at hand _not_ being among them, which explains the effectively statement-terminating error (uncaught exception).

https://github.com/PowerShell/PowerShell/blob/697dc5b37149d0dd98c34c33a87c833a23fe467e/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs#L127-L163

Sounds like all that is needed is to add a catch-all catch branch.

PR is pulled but I don't know that causes the exception to add a test.

:tada:This issue was addressed in #11944, which has now been successfully released as v7.1.0-preview.1.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings