Drag and Drop to a Powershell script

I realize this question is a couple of years old now, but since it’s still the top result for a lot of Google queries relating to running PowerShell via an Explorer drag-and-drop I figured I’d post what I came up with today in the hopes it helps others.

I was wanting to be able to drag-and-drop files onto PowerShell (ps1) scripts and couldn’t find any good solutions (nothing that didn’t involve an extra script or shortcut). I started poking around the file associates in the Registry, and I came up with something that seems to work perfectly.

First we need to add a DropHandler entry for .PS1 files so that Explorer knows PS1 files should accept drag-drop operations. Do that with this Registry change. You’ll probably have to create the ShellEx and DropHandler subkeys.

HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\ShellEx\DropHandler\
(Default) = {60254CA5-953B-11CF-8C96-00AA00B8708C}

This drop handler is the one used by Windows Scripting Host. It’s my first choice because it supports long filenames. If you run into trouble with spaces or something else, you can try the standard Shell32 executable (.exe, .bat, etc) drop handler: {86C86720-42A0-1069-A2E8-08002B30309D}.

Both of these drop handlers work by simply invoking the default verb (what happens when you double-click a file) for the file type (seen as the (Default) value of the Shell key). In the case of PS1 files, this is Open. By default this verb displays the PowerShell script in Notepad — not very helpful.

Change this behavior by modifying the Open verb for PS1 files:

HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command\
(Default) = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit -File "%1" %*

This will run the script in PowerShell when opened as well as pass it all the parameters (dropped files). I have it set to stay open after the script completes, but you can change this by removing the -NoExit option.

That’s it. I haven’t done any extensive testing, but so far it seems to be working very well. I can drop single files/folders as well as groups. The order of the files in the parameter list isn’t always what you’d expect (a quirk of how Explorer orders selected files), but other than that it seems ideal. You can also create a shortcut to a PS1 file in Shell:Sendto, allowing you to pass files using the Send To menu.

Here’s both changes in REG file format:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\ShellEx\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -File \"%1\" %*"

Notes:

  • As a result of these changes double-clicking a PS1 file in Explorer will execute the script; to edit instead of open you’ll have to use the right-click menu. And just as a suggestion (sadly learned from bitter experience :), you might consider a confirmation/sanity-check guard if you have scripts which take damaging actions.

  • Scripts run via drag-drop actions will have a default starting working directory of C:\Windows\system32\. Another reason to be careful.

  • Remember you will need to change your Execution Policy (Set-ExecutionPolicy) unless you’re using signed scripts.

  • If you have adjusted handling of .PS1 files on Windows 10, you need to delete the registry key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\UserChoice which will override the default handling.

Leave a Comment