2

I just learned about Powershell, I've been searching for more than 24 hours and reading a lot of articles, but I still haven't found a way to solve the following problems (because I'm not smart), hope someone can help.

1. How to remove blank line in output of format-list?

I wrote the following code to check the hash of a file:

powershell -noexit Write-Host '%1';get-filehash -literalpath '%1' -algorithm MD5 | format-list -property algorithm, hash; get-filehash -literalpath '%1' -algorithm SHA1 | format-list -property algorithm, hash

The following results

D:\Downloads\new 1.txt


Algorithm : MD5
Hash      : B1D91A5CB33FDCD3CF24964769E23BDA





Algorithm : SHA1
Hash      : 748B44A79C7FA38898C0248DE6D1B102A71B36D3

There are 5 blank lines in between the two results, how can I shorten it to just 1 line?

I also tried using select-object like this, the result was nice, but for hash functions like SHA512, it didn't show the full range but instead had "..." marks at the end.

powershell -noexit Write-Host '%1';get-filehash -literalpath '%1' -algorithm MD5 | select-object -property algorithm, hash;get-filehash -literalpath '%1' -algorithm SHA1 | select-object -property algorithm, hash;get-filehash -literalpath '%1' -algorithm SHA256| select-object -property algorithm, hash

Please note:

  1. I need the code on one line to add to the registry.
  2. I saw an answer here, but it was too complicated for me. I don't know what to do to incorporate it into my code: Remove empty lines after simple Format-list command

2. How to concurrently: display results in Powershell, copy to clipboard and output txt file?

If I use "| clip" after format-list, it doesn't work.

powershell -noexit Write-Host '%1';get-filehash -literalpath '%1' -algorithm MD5 | format-list -property algorithm, hash;get-filehash -literalpath '%1' -algorithm SHA1 | format-list -property algorithm, hash | clip 

3. Output file

I can't output file to C drive (or does anyone know any other way?), so how can Powershell output file to another available drive? For example, your D drive is locked, Powershell will recognize that and output file to drive E.

Or is there a way to open notepad and automatically paste the results there, then you can choose to save the file or not?

Thank you very much for all the help.

mklement0
  • 382,024
  • 64
  • 607
  • 775
NqHai
  • 33
  • 5
  • 2
    DO NOT use the `Format-*` cmdlets for anything other than FINAL output to the screen or a plain text file. ///// instead, use `Select-Object` or `[PSCustomObject]` to build the object you want with the properties that you want ... and either export to a CSV or write each object to a file with `Add-Content`. – Lee_Dailey Apr 21 '22 at 00:43

3 Answers3

2

if all you want is a kinda-sorta "one liner" that outputs the algo & hash to a single line, something like this will work [grin] ...

the code ...

Get-ChildItem -LiteralPath $env:TEMP -Filter '*.log' -File | Get-FileHash -Algorithm MD5 | ForEach-Object {'Algo = {0}; Hash = {1}' -f $_.Algorithm, $_.Hash}

what it does ...

  • grabs the files to work with
    i chose the log files from my temp dir.
  • pipes each to Get-FileHash with the desired algo
  • pipes that to ForEach-Object
  • builds a string with the -f string format operator
  • sends that out to the screen
    you can send it elsewhere as needed.

the output ...

Algo = MD5; Hash = B8D9CD8AEAC35E8C018A9F612196D2F7
Algo = MD5; Hash = 4FF28D9F2ABAC624DC04EA690B613509
Algo = MD5; Hash = FC738758B9ECA9E36BB15C8AEA592BEC
Algo = MD5; Hash = 1C480823E74B761DF4BEE1A5684ACD80
Algo = MD5; Hash = 23088A1A7FB398B077DF7CB52F9925DD
Algo = MD5; Hash = 8679AAAC06D4EE1E80E28C356E650189
Algo = MD5; Hash = 42AB574F9F8D5735E81A0164F1942C20
Algo = MD5; Hash = 36D72201A2B33DC2103B9CEA62502CCF
Algo = MD5; Hash = 730770FAC0461041C756DF7BB500729A
Algo = MD5; Hash = E388F1B0827265FE5BB2DB888A6737A1
Algo = MD5; Hash = 3CFA940E581F8945F7863F4F0DE6F5D9
Algo = MD5; Hash = 8B7BD3ADA2BBF862A486D557BF96A770
Algo = MD5; Hash = 5C7DE92E6914A7FF41A70C30313C2740

i am a tad confused that you don't want the file name.

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • 1
    Thanks very much. I also just found another way, it worked. $md5=(Get-FileHash -path "D:\Downloads\Test.txt" -Algorithm MD5).Hash; Write-Host "MD5: $md5"; $sha1=(Get-FileHash -path "D:\Downloads\Test.txt" -Algorithm SHA1).Hash; Write-Host "SHA1: $sha1"; $sha256=(Get-FileHash -path "D:\Downloads\Test.txt" -Algorithm SHA256).Hash; Write-Host "SHA256: $sha256" – NqHai Apr 21 '22 at 01:41
  • 1
    @NqHai - you are quite welcome! glad to know that you got things working as needed ... [*grin*] – Lee_Dailey Apr 21 '22 at 01:44
  • I would like to ask further if you don't mind, how can I output Get-FileHash results to clip and txt in just one command line? – NqHai Apr 21 '22 at 02:49
  • 1
    @NqHai pipe to `tee-object` – Santiago Squarzon Apr 21 '22 at 04:07
  • 1
    @NqHai - as SantiagoSquarzon pointed out, you can use `Tee-Object`, but you can also save the info to a $Var, and take two steps - the 1st = pipe to `Set-Clipboard`; 2nd = pipe to other location [file/screen/whatever]. – Lee_Dailey Apr 21 '22 at 15:41
  • Thanks very much. – NqHai Apr 22 '22 at 17:31
  • @NqHai - you are most welcome! [*grin*] – Lee_Dailey Apr 22 '22 at 17:58
1
#just simple foreach

Gci -File C:\Temp.txt | % { Get-FileHash $_.Fullname -Algorithm MD5; Get-FileHash $_.Fullname -Algorithm SHA1}

#object using select object

$Path = Get-ChildItem C:\Temp
foreach ($i in $Path){
"" | Select-Object @{name='FileName';Expression={($i.FullName)}},
@{Name='MD5';Expression={(Get-FileHash $i.FullName -Algorithm MD5).Hash}},
@{Name='SHA1';Expression={(Get-FileHash $i.FullName -Algorithm SHA1).Hash}}

}
  • I have tried, the results still show incomplete with long hashes like SHA512. But I already know one more way to use Select-Object, thanks a lot. – NqHai Apr 21 '22 at 01:15
1

You can use Tee-Object to output to a file, and back into the pipeline. Other than that, here's my take on this:


PowerShell.exe -Command "
    'MD5','SHA1','SHA256','SHA512' | 
    Foreach-Object { Get-FileHash -LiteralPath 'C:\Users\Abraham\Desktop\atari\cleanmem\New Text Document.txt' -Algorithm `$_ } | 
    Select-Object -Property 'algorithm', 'hash' | 
    Tee-Object -FilePath 'C:\Temp\temp.txt' | Format-List -Property * | clip; Get-Clipboard"

In this situation, I prefer the use of Clip.exe rather than Set-Clipboard as it sends the string as a whole, and not as an object; this would result in hashtable sets in your clipboard whereas Clip wouldn't. Finally, after setting to the clipboard, you can get the contents back from it using Get-Clipboard. This results in:

Algorithm : MD5
Hash      : D41D8CD98F00B204E9800998ECF8427E

Algorithm : SHA1
Hash      : DA39A3EE5E6B4B0D3255BFEF95601890AFD80709

Algorithm : SHA256
Hash      : E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855

Algorithm : SHA512
Hash      : CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD474
            17A81A538327AF927DA3E
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24