Home > Software engineering >  Get-FileHash piped from Resolve-Path not working as expected
Get-FileHash piped from Resolve-Path not working as expected

Time:11-11

What am I missing here? This command works as expected:

Get-ChildItem -Path .\subdir\ -Recurse -Filter *.* | Resolve-Path -Relative

and so does this:

Get-ChildItem -Path .\subdir\ -Recurse -Filter *.* | Resolve-Path -Relative | Sort

But the following one fails:

Get-ChildItem -Path .\subdir\ -Recurse -Filter *.* | Resolve-Path -Relative | Get-FileHash

Edit: What I am trying to achieve is to get relative path of files in the final output.

Thanks in advance!

CodePudding user response:

According to the docs, in powershell 5.1, only literalpath or its alias pspath can be piped to get-filehash. And apparently also only by property name, not by value, which doesn't seem to appear in the docs anymore. Your code would work ok in powershell 7.

[pscustomobject]@{literalpath='there'} | get-filehash

Algorithm Hash                                                Path
--------- ----                                                ----
SHA256    73253E1BB32AE0BE82342FA1CEA0A653CC84097CD65D6DE9... C:\users\js\foo\there

CodePudding user response:

  • Don't use -Filter *.* if your true intent is to match files only - files can lack an extension (e.g. file) and, conversely, directories may have one (e.g. dir.foo)

  • Don't use Resolve-Path -RelativePath if your intent is to pipe file-information objects to Get-FileHash.

    • Instead, pipe the System.IO.FileInfo instances output by Get-ChildItem directly to Get-FileHash.
Get-ChildItem -Path .\subdir\ -Recurse -File | Get-FileHash

Even though the intermediate Resolve-Path -Relative path does work in PowerShell (Core) 7 - but not in Windows PowerShell - there is no benefit to using it, because the .Path property of the objects output by Get-FileHash always contain the full path.

  • Related