Get hash from files/folders and subfolders into one csv file

Ed7 96 Reputation points
2021-11-30T09:26:48.153+00:00

Hello,

I know that with powershell we can get the hash (MD5, etc) from files/folders and subfolders. I was able to do it with to get one single folder and its content.

I would like to know/get a script or having help how to write a script that gets the hash from several files/folders finto only one csv file instead of having multiple csv files from each folders.

I am struggling with this and any support would be much appreciatted.

Thank you

Microsoft 365 and Office Development Other
Microsoft 365 and Office Excel For business Windows
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Clément BETACORNE 2,496 Reputation points
    2021-11-30T10:17:37.047+00:00

    Hello,

    This should do the job :

    Get-Childitem -path "<folder1>","<folder2>","<folder3>" -recurse | Get-FileHash | Export-csv -path "<myfile>" -Delimiter ";" -NoTypeInformation
    

    Be careful with where you will put <myfile> because it will throw you an error if the file is located in folder1, folder2 or folder3.
    I didn't include error handling

    Regards,

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Jose Antonio Silva 11 Reputation points
    2021-12-03T22:19:10.657+00:00

    @Ed7 - another option for your relative path challenge:

    param (  
         $folders = @("C:\temp_fotos\", "C:\fotos_temp\") #case sensitive  
     )  
       
     $allFiles = foreach($folder in $folders) {  
         Get-Childitem -path $folder -recurse |  
            select FullName,Name,Length |   
            foreach {  
                $hash = Get-FileHash $_.FullName   
                add-member -InputObject $_ -NotePropertyName Hash -NotePropertyValue $hash.Hash  
                add-member -InputObject $_ -NotePropertyName RelativePath -NotePropertyValue $_.FullName.Replace($folder, '') -PassThru        
            }  
     }  
      
     $allFiles | select -First 10 | ft RelativePath, Hash   
    

    154903-hash.png

    1 person found this answer helpful.

  2. Ed7 96 Reputation points
    2021-11-30T11:02:40.533+00:00

    Hello,

    Thank you for this.

    It seems working as expected, however, all the information is whithin the same column and I need to split in diferent collums.

    Also I need to have a relative path so when comparing files copied from one server tro another to avoid any error with paths and focus on the files/folders.

    How can I do that?

    Apologises as I am new on this.

    Thank you


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.