Drag and Drop to a Powershell 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
:More
shift
if '%1'=='' goto Done
set args=%args%, %1
goto More
:Done
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"
$args
$args.Gettype().Fullname
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.
The secret all this time, since it was made available, has been the parameter attribute "ValueFromRemainingArguments".
Shortcut, Batch or CMD file to receive the dropped files
Version 5.1 or earlier
powershell.exe -noexit -file c:\Test.ps1
Powershell 6 and newer
pwsh.exe -noexit -file c:\Test.ps1
Test.ps1
[CmdletBinding()]
param (
[Parameter(ValueFromRemainingArguments=$true)]
$Path
)
$Path
TestExpandDirectories.ps1
[CmdletBinding()]
param (
[Parameter(ValueFromRemainingArguments=$true)]
$Path
)
foreach ($aPath in $Path) {
$aPath
if (Test-Path $aPath -PathType Container) {
Get-ChildItem $aPath -Recurse | Select-Object -ExpandProperty FullName
}
}
Also, if the script does not have a param block and is ran with Powershell -File ScriptName.ps1, the $args variable will contain exactly what is expected, an array of file paths.