Search code examples
powershellhashconcatenationget-childitemgetcontent

Powershell create a sort of hash table of the contents of text files and their corresponding file names


I am trying to get the content of text files containing md5 check sums and their corresponding file names.

Example:

file1.iso.md5 contains the checksum "g3d8d3d128200fa20a07e81c90f5f367"

file2.iso.md5 contains the checksum "97e4f28b330a01c02d839367da634299"

I would like to get another text file that prints all checksums next to the corresponding file name in one document.

N.B. The text files are named identical to the actual files to which the checksums included belong, i.e. there is a file "file1.iso" plus a text file "file1.iso.md5".

Please also note that I am limited to running code (one-liners) in the shell directly only. I must not execute ".ps-files"!

(I am using shift + right click in the respective folder and opening PowerShell from the context menue.)

So far, I could only either get a simple list of files using "dir > dir.txt", then copying it to an excel sheet and deleting the files, I don't need manually. Then, I would run Get-Content .\*.md5 | Out-File .\Concat.txt.

Then, I can copy each line next to its corresponding file name, however this doesn't give me much of an advantage over opening each file individually and then copying the content to the excel sheet.

What it boils down to is that I want to create a simple hash table from contents of individual files.

I hope it is possible to achieve without saving a script as .ps first and then running it.

Thanks a lot for looking into this.


Solution

  • Please try this

    gci|get-content|Select @{l='contents';e={$_}}, @{l='file'; e={$_.PSChildName}}, @{l='path';e={$_.PSParentPath}}
    

    A tip for the future issues. You can pipe most of the commands to get-member. In this case please take a look at the Property items.