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:
- I need the code on one line to add to the registry.
- 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.
CodePudding user response:
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.
CodePudding user response:
#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}}
}