Reputation: 67
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:
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.
Upvotes: 2
Views: 260
Reputation: 4694
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
Upvotes: 1
Reputation: 821
#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}}
}
Upvotes: 1
Reputation: 7479
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 ...
Get-FileHash
with the desired algoForEach-Object
-f
string format operatorthe 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.
Upvotes: 2