Link to home
Start Free TrialLog in
Avatar of Rich Rumble
Rich RumbleFlag for United States of America

asked on

Drag and drop powershell file hashing script

PowerShell drag and drop, then hash the file that was dropped onto the form.
I can't figure out where to add something like "$fileHash = Get-FileHash -path $file -A SHA256" and then append to the file path.
The code below shows the file path when dragging a file, I want to hash that same file and add it's hash to the end
c:\some\file\path\here.txt, F70363B8AE41B113AC96F9762DB46262B8F436B29A4B7D1E2A3ADAB101E75299
I may further call a VirusTotal API call to look the hash up and append more info on the end later.
Right now all I need is the hash of the file(s) that get drug onto the form.

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = "391, 190"
$Form1.TopMost = $true
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Anchor = "Top,Bottom,Left,Right"
$TextBox1.Location = "12, 12"
$TextBox1.Multiline = $true
$TextBox1.ScrollBars = "Both"
$TextBox1.Size = "368, 166"
$TextBox1.AllowDrop = $true
$TextBox1.add_DragEnter({FNprocess($_)})
$Form1.Controls.Add($TextBox1)
function FNprocess( $object ){
  foreach ($file in $object.Data.GetFileDropList()){
    $TextBox1.AppendText($file+[char]13+[char]10)
  }
}
[System.Windows.Forms.Application]::Run($Form1)

Open in new window

Avatar of oBdA
oBdA

Just use the function where you're filling the text box.
LoadWithPartialName is somewhat dated. btw:
Add-Type -AssemblyName "System.Windows.Forms"
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = "391, 190"
$Form1.TopMost = $true
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Anchor = "Top,Bottom,Left,Right"
$TextBox1.Location = "12, 12"
$TextBox1.Multiline = $true
$TextBox1.ScrollBars = "Both"
$TextBox1.Size = "368, 166"
$TextBox1.AllowDrop = $true
$TextBox1.add_DragEnter({FNprocess($_)})
$Form1.Controls.Add($TextBox1)

Function FNprocess($object) {
	ForEach ($file in $object.Data.GetFileDropList()){
		$hash = (Get-FileHash -Path $file -A SHA256).Hash
		$TextBox1.AppendText("$($file), $($hash)`r`n")
	}
}
[System.Windows.Forms.Application]::Run($Form1)

Open in new window

Avatar of Rich Rumble

ASKER

That works! Seem obvious when you guys do this so quickly... if you have suggestions on improvements I'm all for it. I was looking for something simple to build from is all.
-rich
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As always, great solution, thanks! You know I'll be back soon :)
-rich