Drag and Drop to a Powershell script

I thought I had an answer to this, but the more I play with it, the more I see it as a design flaw of Powershell.

I would like to drag and drop (or use the Send-To mechanism) to pass multiple files and/or folders as a array to a Powershell script.

Test Script

param ( [string[]] $Paths, [string] $ExampleParameter )

Attempt #1

I created a shortcut with the following command line and dragged some files on to it. The files come across as individual parameters which first match the script parameters positionally, with the remainder being placed in the $args array.

Shortcut for Attempt #1

powershell.exe -noprofile -noexit -file c:\Test.ps1

Wrapper Script

I found that I can do this with a wrapper script...

& .\Test.ps1 -Paths $args

Shortcut for Wrapper Script

powershell.exe -noprofile -noexit -file c:\TestWrapper.ps1

Batch File Wrapper Script

And it works through a batch file wrapper script...

REM TestWrapper.bat
SET args='%1'
IF '%1' == '' GOTO Done
SET args=%args%,'%1'
Powershell.exe -noprofile -noexit -command "& {c:\test.ps1 %args%}"

Attempted Answers

Keith Hill made the excellent suggestion to use the following shortcut command line, however it did not pass the arguments correctly. Paths with spaces were split apart when they arrived at the Test.ps1 script.

powershell.exe -noprofile -noexit -command "& {c:\test1.ps1 $args}"

Has anyone found a way to do this without the extra script?

Running powershell scripts by drag-n-drop file or folder on it, Create a Windows Explorer shortcut (In Explorer right click -> new -> shortcut). Then right-click on your shortcut and open the Properties dialog� Enter this as the target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file C:\YourScriptDirectory\Compress.ps1 Now when you drop files and folder on this shortcut, the files will be passed as the first argument in the script.

The easiest way to pass files or folders to a Powershell script is a wrapper script like the following:

@echo off
rem the name of the script is drive path name of the Parameter %0
rem (= the batch file) but with the extension ".ps1"
set PSScript=%~dpn0.ps1
set args=%1
if '%1'=='' goto Done
set args=%args%, %1
goto More
powershell.exe -NoExit -Command "& '%PSScript%' '%args%'"

All you have to do is

make a copy of the .bat File

give it the same name as the script file but with the extension .bat

For example "hello.ps1" <--> "hello.bat"

Drop the files/folders onto the batch file and it will pass them to the script.

A sample script code may look like this:

"hello world"

Allow the drag and drop of a file into a script : PowerShell, Basically is there a way to script that and user (i.e without editing registry for drop handlers) can either drag and drop a csv onto the ps1 and it accepts it as the� If you are developing this application in PowerShell Studio, you will need to first export the code, either to a standalone EXE, or .PS1 file. Run the application from either of the above. Drag a file of you choice from explorer to the textbox. The textbox will be populated with the filepath of the file that has been dragged and dropped over it.

The secret all this time, since it was made available, has been the parameter attribute "ValueFromRemainingArguments".


powershell.exe -noexit -file c:\Test.ps1


param (


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. The script below shows how to create a simple WinForms GUI in PowerShell, where you can drag and drop files and folders and then process these with PowerShell commands when you click the button. I’ve pieced this together using various online source and my own trial and error, and figured it might be useful as a base for others who need to

Change your shortcut to this and try it:

powershell.exe -noprofile -noexit -command "& {c:\test1.ps1 $args}"

Using Drag and Drop in PowerShell GUIs, For this particular script, we'll implement drag and drop functionality to allow us to drag a file from explorer to a textbox in we've created. When the� C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -noprofile -file C:\yourscriptdirectory\yourscript.ps1 Whether you need the -noexit or -noprofile options is up to you, take a look at the help to decide. Now when you drop a file (or folder) onto this shortcut, explorer will magically pass it as the first argument to your script.

Unfortunately, $args will split your arguments on spaces, thus working incorrectly on filenames containing them. However, you still can process $args inside your .ps1 without an external wrapper script!

Since everything you drag-n-drop on a shortcut (or select for "Send To") will be passed to your script as space-joined list of absolute paths, you can parse $args for all absolute path occurrences using regex and pipe it to foreach:

$pattern = '([a-zA-Z]:\\(((?![<>:"/\\|?*]).)+((?<![ .])\\)?)*)(\s|$)'

Select-String "$pattern" -input "$args" -AllMatches | Foreach {$_.matches} | ForEach-Object { 
    $path = "$($_.Groups.Groups[1].Value)"
    # ...

Where each $path is exactly one absolute path (including those with spaces inside)

The absolute file pattern is taken from here.

I wrapped it in parentheses (to match the first group with the actual path) and added a (\s|$) group at the end, which is "whitespace or end of string", needed because shortcut arguments will be joined with single spaces; $ is required to match the last path occurrence in the argument list.

A drag-and-drop GUI made with PowerShell, The script below shows how to create a simple WinForms GUI in PowerShell, where you can drag and drop files and folders and then process� I am limiting drag and drop actions to a WPF listbox control in Powershell to only allow text files to be dropped. I would like to use the System.Windows.DragDropEffects property to prevent the drop action on the DragEnter event as it also changes the mouse cursor providing user feedback for the denied drop action.

How to make a Powershell script which cuts a document and pastes , Powershell doesn't have drag & drop operations setup automatically. In order to accomplish that, you'd need to make a couple registry changes, and I'm not sure � Drag and Drop to a Powershell script (4) I thought I had an answer to this, but the more I play with it, the more I see it as a design flaw of Powershell. I would like to drag and drop (or use the Send-To mechanism) to pass multiple files and/or folders as a array to a Powershell script.

Drag and Drop Form for Powershell – Alan's Blog, Get-DragAndDrop.ps1 is drag and drop PowerShell form is based on looks like this: Screen Capture for Drop and Drag Function Script Text� With VBScript files, you can drag-n-drop another file on top of the VBScript. file. This executes the VBScript and then the path of the dropped file is. passed in as an argument to the script. You can also put VBScript scripts into your Send To folder in your local. profile, then "send" files to the script.

PowerShell Drag & Drop sample � GitHub, PowerShell Drag & Drop sample. # Usage: # powershell -sta -file dragdrop.ps1. # (-sta flag is required). #. Function DragDropSample() {. Drag a text file onto it. It opens. Open notepad as an administrator, try to drag a file. Won't work. You should be able to drag between 2 programs running elevated, but as far as I know explorer never runs elevated, even when you explicitly tell it to, so you'd have to be dragging from some other application that works as a drag source.

  • Here is an alternative solution although it would need to be adapted to powershell
  • For the record... If the script is ran with Powershell -File ScriptName.ps1 and the script does not have a param block, the $args variable will contain exactly what is expected, an array of file paths.
  • Here's a bug report you can upvote on Powershell's UserVoice.
  • Nice job! That would certainly do the trick. I've been getting by with cmd.exe batch file stub scripts and will probably continue. Because I need to make sure anyone can do these tasks, without requiring them to tweak their PC's default .PS1 handling. ... A year older and a year more conservative.
  • On Windows 8+, make sure you add Windows Registry Editor Version 5.00 as the first line of the .REG file
  • Not the easiest, but might be the only way until Microsoft fixes what I believe to be a bug.
  • Is there any reason not to use the one-liner powershell.exe -NoExit -Command "& '%~dpn0.ps1' %*" instead of looping through the arguments? It doesn't handle paths containing spaces, but as far as I can tell, neither does the above batch script.
  • @billyjmc true, but you could adapt @thomas-jüttner answer with set args='%~1' as well as set args=%args%, '%~1' (and call powershell.exe with "& '%PSScript%' %args% ") me thinks
  • No need for a wrapper script. Take a look at the accepted answer... stackoverflow.com/a/45838761/80161
  • Sorry, it was an elegant answer, but unfortunately, the paths arrive split by any white space in them. I've updated the question to reflect this attempt.
  • No, $args is a string[] per my testing. What you are probably seeing is that if you render a string array to a string, PowerShell uses the value of $OFS to separate each entry of the array. If $OFS isn't set then PowerShell uses a space as a separator. In your script, try setting $OFS = ', '.
  • Yes $args is an array, but the problem is not being caused by array expansion. When dragging files on a shortcut, Windows passes the paths separated by spaces and wrapped in quotes when necessary. Your shortcut passes the files a second time, this time without the quotes. What the script receives is a long string which it then parses by splitting it at any white spaces. I liked your idea and even tried splatting the arguments ( @args ), wrapping them as an array ( @($args) ) and adding the $OFS solution to the command line, to no avail. If you try it, you will see what I mean.