We help IT Professionals succeed at work.

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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
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

Rich RumbleSecurity Samurai
CERTIFIED EXPERT
Top Expert 2006

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
The usual behavior that you actually have to drop the file would probably be an improvement, as far as expected behavior is concerned.
$event_DragEnter = {
	Param($Sender, $EventArgs)
	$EventArgs.Effect = [System.Windows.Forms.DragDropEffects]::All
}
$event_DragDrop = {
	Param($Sender, $EventArgs)
	ForEach ($file in $EventArgs.Data.GetFileDropList()){
		$hash = (Get-FileHash -Path $file -A SHA256).Hash
		$TextBox1.AppendText("$($file), $($hash)`r`n")
	}
}

[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($event_DragEnter)
$TextBox1.add_DragDrop($event_DragDrop)
$Form1.Controls.Add($TextBox1)
[System.Windows.Forms.Application]::Run($Form1)

Open in new window

Rich RumbleSecurity Samurai
CERTIFIED EXPERT
Top Expert 2006

Author

Commented:
As always, great solution, thanks! You know I'll be back soon :)
-rich