1

I have foreach loop that go through file, get path and replace it in the command for getting hashes:

$hash = @{}
...
defining variables here
...
foreach($line in $lines)
{
    Get-FileHash $line -Algorithm md5 | format-list | Where { $_ } |  Out-File -Width 200 'hash.txt' -Encoding ascii -Append
    Get-FileHash $line -Algorithm sha256 | format-list | Where { $_ } |  Out-File -Width 200 'hash.txt' -Encoding ascii -Append
    Get-FileHash $line -Algorithm sha512 | format-list | Where { $_ } | Out-File -Width 200 'hash.txt' -Encoding ascii -Append
    @('MD5','SHA256','SHA512') | % { Get-FileHash -Algorithm $_ $line } |  export-csv hash.csv -Delimiter "," -Append
}

The problem which I'm facing with is that I have 2 empty lines on the beginning of file, and 4 empty lines on the end of file, here is the output:



Algorithm : MD5
Hash      : xxx
Path      : xxx

Algorithm : SHA256
Hash      : xxx
Path      : xxx

Algorithm : SHA512
Hash      : xxx
Path      : xxx




How should I get rid of empty lines on the beginning and the end of file?

marsze
  • 15,079
  • 5
  • 45
  • 61
Nemanja
  • 45
  • 3

1 Answers1

1

You could format all output in one step, and then trim it before writing to file:

($lines | foreach {
      foreach ($alg in ('MD5','SHA256','SHA512')) {
          Get-FileHash $_ -Algorithm $alg
      }
} | format-list | out-string).trim() | Out-File 'hash.txt' -Encoding ascii
marsze
  • 15,079
  • 5
  • 45
  • 61