how to combine Get-ChildItem and Get-FileHash output? powershell

1.1k Views Asked by At

I want to get table like name-algorithm-hash for files, ended by .gz in folder

a filtration work well: powershell -command " Get-ChildItem -Filter 'L04\*.gz' | Select Name" give a table:

Name
v300040828_run20_L04_62_1.fq.gz 
v300040828_run20_L04_62_2.fq.gz

But upgreid command with hash powershell -command " Get-ChildItem -Filter 'L04\*.gz' | ls | Get-FileHash -Algorithm MD5| Select Name,Algorithm,Hash" give me only alg and hash without name..

   Name Algorithm Hash
      MD5 08B622CFAB1254DE77DEE2B86B8566C5
      MD5 BB0ACF3AE1F9E67BAF8F64736221D401

Help me please to get also name

2

There are 2 best solutions below

0
On BEST ANSWER

Get-FileHash returns an object with properties Path, Algorithm and Hash.
Path being the file's FullName.

If you want that changed, you can do

Select-Object @{Name = 'Name'; Expression = {[System.IO.Path]::GetFileName($_.Path)}}, Algorithm, Hash
0
On
$GCIArgs = @{Path   = "G:\BEKDocs\Scripts\DiskSpaceGUI"
             Filter = '*.ps1'}
Get-ChildItem  @GCIArgs |
  Get-FileHash -Algorithm MD5|
  Select-Object Algorithm,Hash,@{
      n="Name";e={($_.Path).Split("\")[-1]}}

Results:

Algorithm Hash                             Name                   
--------- ----                             ----                   
MD5       FDD38964CA6EC73C01FB5097E8EFFE9E DiskSpaceGUI-V1-0.ps1  
MD5       2991850091CA66634BC9287E00FB086C DiskSpaceGUI-V2-0.1.ps1
MD5       2991850091CA66634BC9287E00FB086C DiskSpaceGUI-V2-0.ps1  
MD5       A9ECA8BF38D57D953E51C11D3F881A4F DiskSpaceGUI-V2-1.ps1  
MD5       E265F9DCCC1BE106C71789F6040DBAEB DiskSpaceGUI-V2-3.ps1  
MD5       FB287373331EB4716FAF0CFC99FF2390 DiskSpaceGUI-V2-4.ps1  
MD5       B370CD26B814B63A5779CB4BD1630E9F DiskSpaceGUI.ps1       

HTH