question

Ed7 avatar image
0 Votes"
Ed7 asked joseantoniosilva commented

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

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-serverwindows-server-powershellpower-query-not-supportedoffice-excel-itprooffice-scripts-excel-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ClementBETACORNE avatar image
0 Votes"
ClementBETACORNE answered

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,

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ed7 avatar image
0 Votes"
Ed7 answered Ed7 edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can change the delimiter if you want by default I'm always using ";" but you can use ",".
Regarding the relative path you want to have only the parent of the file or the root folder ?

Regards,

0 Votes 0 ·
Ed7 avatar image Ed7 ClementBETACORNE ·

I am going to check for several folders and its contents how can use the variable?
Such as this: $variabel1 = "<folder1>","<folder2>","<folder3>"
How can I have multiple folders to extract the content in one variable or do I have to have multiple variables?

I need to construct a relative path from the directory at the top of the tree...
For example, if the file in question lives at absolute path C:\Users\MyUsername\Desktop\test.txt, and I decide that the root directory is C:\Users\MyUsername, the relative path would be Desktop\test.txt, which is what should be stored in the CSV.

But I do not know how to write the script for that or what is missing




153767-image.png


[1]: /answers/storage/attachments/153783-image.png


0 Votes 0 ·
image.png (1.7 KiB)
joseantoniosilva avatar image
0 Votes"
joseantoniosilva answered joseantoniosilva commented

@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




hash.png (37.7 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ClementBETACORNE :Thanks its worked for me as well.....!! Could you also provide the commad to get hash value output to csv file with date & time stamp

0 Votes 0 ·

In line 7 above, just add any other System.IO.DirectoryInfo property. e.g.

 select FullName,Name,Length,CreationTime,LastWriteTime |

0 Votes 0 ·