Forumbits: Drag-and-Drop Files into Powershell Script

Recently in the German Technet Powershell forum, the question came up on how to drag files into a Powershell script in order to process them.

As you can’t drag files directly onto a .ps1-file, one way is to use a wrapper-script.
But I prefer using a GUI for tasks like this. And in this case, its actually quite simple.

First we create a simple Windows form with a textbox:

 [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"

 $Form1.Controls.Add($TextBox1)

 [System.Windows.Forms.Application]::Run($Form1)

Now we need to add an event, that will be raised when you drag one or more files onto the textbox.
So we choose “DragEnter” and define that when the event is raised, the function “FNprocess” will be called and the event data ( “$_”) will be passed to the function.

 $TextBox1.add_DragEnter({FNprocess($_)})

We also need to allow DragDrop-events for the textbox:

 $TextBox1.AllowDrop = $true

And finally, we create the function “FNprocess”.
Luckily, the data object we get from the event data, contains a method “GetFileDropList”, which provides a collection of the paths of the dropped files. So we simply parse the collection and add each file path in the list to the textbox, together with a line break :

 function FNprocess($eventdata){
   foreach ($file in $eventdata.Data.GetFileDropList()){
      $TextBox1.AppendText($file+[char]13+[char]10)
   }
 }

The final script looks like this:

[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)

And this is the result:

Now that we got the file paths, we could of course easily read the contents of some files or process them in any other way.

There is one catch though. And that is in order for drag-and-drop actions to work, the script needs to run in the Single-Threaded-Apartment mode. That means that you have to create a shortcut to your .ps1-file, open the shortcut properties and add the following under “Target”, in front of the script path: “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” -sta

Alternatively, you can run the script in Powershell ISE, which will run it automatically in STA mode.

Have a nice day,
Denniver

  1. Converting Playlists from Windows to *nix | Shane Skriletz

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: