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

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,208 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,537 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,291 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,649 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
0 comments No comments
{count} votes

Accepted answer
  1. Clément BETACORNE 2,031 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. 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


  2. Jose Antonio Silva 6 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