Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort results from Get-ChildItem with Get-FileHash before output

I try to write a windows PowerShell script. I need to get file hash from all files in directory tree.

This is what I got so far:

Get-ChildItem -Path "c:\temp\path" -Recurse -Force -Attributes !Directory | % {Get-FileHash $_.Fullname} | Out-File "c:\temp\report_file.txt"

File c:\temp\report_file.txt is something like this:

Algorithm Hash Path                                                                         
--------- ---- ----                                                                         
SHA256 E3B0C44298...E4649B934CA495991B7852B855 c:\temp\path\report1.txt                                                        
SHA256 7B989C1C95...6756624B3887E501DCC377DB23 c:\temp\path\report2.txt                                                       
SHA256 EA0155401C...A6D44F1DEBB95E401AEFF4F908 c:\temp\path\report3.txt                                                      
SHA256 06DAA0E452...32E3F3104EA4564EAB67CA6A0A c:\temp\path\report4.txt                                                     
**SHA256 9C7C9FEA96...45F460BA9015C8F0A5CA830B6B c:\temp\path\report5.txt**              

All works fine.

Expect:
I run this cmdlet a lot of times per day. Files are deleted and re-created from time to time in this directory tree. And... Several times order of files in output file is not the same. In the example below file report5.txt in report file must be in last line, but it is on second line. I suppose it is because of recurse option is selected. This recurse option is needed for me. When I run cmdlet on directory with no subdirectories, result is equal all the time. But when on directory with subdirectories (directory tree) - not.

Algorithm Hash Path                                                                         
--------- ---- ----                                                                         
SHA256 E3B0C44298...E4649B934CA495991B7852B855 c:\temp\path\report1.txt                                                        
**SHA256 9C7C9FEA96...45F460BA9015C8F0A5CA830B6B c:\temp\path\report5.txt**              
SHA256 7B989C1C95...6756624B3887E501DCC377DB23 c:\temp\path\report2.txt                                                       
SHA256 EA0155401C...A6D44F1DEBB95E401AEFF4F908 c:\temp\path\report3.txt                                                      
SHA256 06DAA0E452...32E3F3104EA4564EAB67CA6A0A c:\temp\path\report4.txt                                                     

Is here a solution to sort somehow all data by column fullpath before data outputed to report file?

like image 883
Betto69 Avatar asked Jan 03 '23 11:01

Betto69


1 Answers

You can sort by the path property of the hash object.

You can also run the files directly into Get-FileHash without using a loop, and I suggest exporting them to CSV instead of text, so it keeps the algorithm, hash and path separated so you can use them more easily:

Get-ChildItem -path "c:\temp\path" -Recurse -Force -File | 
    Get-FileHash | 
    Sort-Object -Property 'Path' |
    Export-Csv -Path "c:\temp\report_file.csv" -NoTypeInformation
like image 158
TessellatingHeckler Avatar answered Jan 14 '23 08:01

TessellatingHeckler