27
votes

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

#Test.ps1
param ( [string[]] $Paths, [string] $ExampleParameter )
"Paths"
$Paths
"args"
$args

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...

#TestWrapper.ps1
& .\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'
:More
SHIFT
IF '%1' == '' GOTO Done
SET args=%args%,'%1'
GOTO More
:Done
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?

9
Here is an alternative solution although it would need to be adapted to powershell - Lime
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. - Nathan Hartley
Here's a bug report you can upvote on Powershell's UserVoice. - Amine I.

9 Answers

36
votes

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.

16
votes

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
14
votes

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
    }
}
2
votes

Change your shortcut to this and try it:

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

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.

0
votes

For those looking for a quick good-enough solution for drag n dropping a single file to a powershell script expecting a string parameter, you can use this as your shortcut:

powershell.exe -noprofile -noexit -command "& {.\myscript.ps1 ($args -join ' ')}"

It could break if you had a file path with multiple consecutive spaces, but this works well for me as I have never encountered such a file where I needed to use this technique.

0
votes

Dragging and Dropping Files onto a PowerShell Script --- Additional Information and Solutions

The solution posted above by Nathan Hartley using a shortcut file works well. (See: stackoverflow.com/a/45838761/80161)

It turns out that you can just as easily use a .BAT file, with the same results, using either to call the same underlying .ps1 script files.

Below is a summary of the two techniques. The sample scripts below have been tested on Windows 10 using both Windows PowerShell 5.1 (powershell.exe) and PowerShell 7.1 (pwsh.exe).

Multiple consecutive spaces in file and directory names are preserved with either technique below, which was a problem with some of the other above mentioned solutions. (Nathan's solution works well in this regard. I include his solution, along with my .bat file solution, in my summary below.)

Sample Shortcut file:

Enter something like the following as the "Target" in the Shortcut's properties:

powershell.exe -noexit -File "C:\Users\User\Documents\PS Scripts\your script.ps1"

You can also simply type powershell or pwsh when entering the above into the Shortcut, assuming the PowerShell program is in your path. When you Save or Apply the Shortcut it will automatically expand the name to a full absolute file name. Also, if the -File parameter path and filename have no spaces you can omit the quotation marks around the called .ps1 script name.

Sample .BAT file:

@echo off
set PSScript=%~dpn0.ps1
powershell.exe -NoExit -File "%PSScript%" %*

As in the posting by another author above, give the .BAT file the same base name as the corresponding PowerShell script. Of course you can, instead, expressly code the called script name into the .BAT file if you prefer, but the above makes it convenient to move the wrapper and .ps1 scripts around as a pair.

The quotes around "%PSScript%" are important, but %* (which passes all parameters passed to the .BAT script to the PowerShell script) must not be enclosed in quotes.

The below example .ps1 scripts can be called by either of the above:

Collecting passed filenames in a 'named' parameter using the 'ValueFromRemainingArguments' specification (this is basically Nathan's example above):

param (
    [Parameter(ValueFromRemainingArguments=$true)]
    $Paths
)

'Paths:'
$Paths

Picking up parameters in the $Args array:

# Simply reference the dropped file names in the $Args array.
# The following will list all the dropped file names.
# Yes, this is a one line .ps1 script!

$Args

The following demonstrates accessing individual passed file names explicitly:

$Args
''
'Example of accessing passed Args individually:'
write-host "=====There are a total of $($args.count) arguments:"
for ( $i = 0; $i -lt $args.count; $i++ ) {
    write-host "Argument  $i is $($args[$i])"
}

Update 6/2021: See follow-up post below for enhanced code that will allow you to include dropped directories as well.

Notes:

  • The [CmdletBinding()] specification shown in Nathan's example is not necessary and is overridden by the [ValueFromRemainingArguments=$true] specification, which causes all remaining dropped file names not already picked up by any preceding parameters to be gathered into the named $Paths parameter to which the specification is attached.

  • The [CmdletBinding()] specification must not be used when accessing the dropped file names via the default $Args variable in the example above, otherwise you'll get an error since this specification demands the all passed parameters have matching parameter specifications.

  • If neither “[CmdletBinding()]” nor “[Parameter(ValueFromRemainingArguments=$true)]” are specified in the PowerShell script and more files are dropped and passed to the PowerShell script than there are named parameters, the file names are passed first via the named parameters, and any remaining file names are pass as $Args parameters, starting with $Args[0].

  • A key element in the solutions above is the use of the -File parameter rather than the -Command parameter in the PowerShell command that calls the respective .ps1 script.

  • As mentioned above, pwsh.exe (PowerShell 7) can be used instead of powershell.exe (Windows PowerShell) in the Shortcut file or Batch file.

0
votes

Dragging and Dropping Files onto a PowerShell Script --- Follow-up: Expanding Dropped Directories

This is a follow-up to my posting above: “Dragging and Dropping Files onto a PowerShell Script --- Additional Information and Solutions”.

The below PowerShell script can be used with either the .bat file or Shortcut file technique described in my earlier posting, and provide additional flexibility in dropping files in Windows Explorer. The script can also be used from the PowerShell Command prompt.

  • This will allow dropping an intermixed list of directory and file names. Directories will be expanded.

  • When run manually from the command line, wildcard characters can also be used to specify files (but not directories).

  • Any passed relative file names will be converted to fully qualified names, which can be important for some applications.

param (
    [Parameter(ValueFromRemainingArguments=$true)]
    $DroppedFiles
)

if ( $DroppedFiles.count -eq 0 ) {
    Write-Host "Supply directory and/or file names. Wildcards are allowed." -ForegroundColor Red
    Exit
}

[bool]$err = $False
foreach ($f in $DroppedFiles) {
    if ( ( [string]::IsNullOrWhiteSpace($f) ) -or ( (Test-Path -Path $f) -eq $False ) ) {
    Write-host "ERROR--File or directory does not exist: " -NoNewline  -ForegroundColor Red
    Write-host $f
    $err = $True
    }
}
if ( $err ) { Exit }

$Droppedfiles = Get-ChildItem -Path $DroppedFiles -File -Depth 0 -Recurse -ErrorAction Stop

$i = 0
foreach ($File in $DroppedFiles) {
    Write-Host "Processing:" $File.FullName
    # ".FullName" was required to display the full absolute path name in Windows Powershell 5.1
    $i++
}
Write-Output "$i file(s) processed."

Notes:

  • This will process multiple intermixed directory and file names.

  • Directories expand only to the first level by default (set by "-Depth 0"). You may increase this or remove the limitation altogether.

  • You can restrict the produced file list by adding an -Include parameter to the Get-ChildItem line, for example: -Include: *.doc,*.docx

  • When calling the script from the command line, any files specified that do not exist return nothing (no error is reported), so a preliminary check for non-existent files or directories is included.

  • Use of the $File.FullName specification in the final foreach loop, in referring to individual dropped file names, was required to display the full filename when the script is run by Windows PowerShell 5.1.

  • The script as written works with Windows PowerShell 5.1 (powershell.exe) and PowerShell 7.1 (pwsh.exe).

Additional Comment

  • If the script will be used only for dropping files from Windows Explorer you should be able to omit the two initial test blocks since Windows Explorer will pass fully qualified names known to exist. If you intend to (or might accidentally) execute the script from the command line the additional tests prevent Get-ChildItem from picking up all files in the current directory by default if no parameter is supplied, and also make sure that any user typed parameters do exist.
0
votes

Function to check and expand file names dropped on a PowerShell Script

Not to belabor the points of my two prior posts, but to simplify the creation of multiple PowerShell scripts upon which I want to be able to drop files and directories, I created a function to do the checks and directory expansions.

Below is what I came up with:

<#
Function: Test+Expand-Dropped-Files
Lewis Newton, 6/2021

This function will process an intermixed list of directory and file names passed to a 
PowerShell script when dropped in Windows Explorer on a properly configured accompanying
Windows shortcut or .bat file.  The PowerShell script or .bat file can also be called directly
from the command line.

-- This function will accept multiple intermixed directory and file names.  
-- It will check that the files exist, and expand any directories recursively to the depth
   specified (it defaults to 0, the top level).
-- The 'Include' parameter allows limiting what is included, for example: -Include *.doc,*.docx
   It defaults to a $Null string array which will include everything found.  
-- Relative file names and wildcard characters are accepted. 
-- The function returns fully qualified file objects.
-- This has been tested with Windows PowerShell 5.1 and PowerShell 7.1.  
   In using the results, note that ".FullName" was required to display 
   the full absolute path name in Windows Powershell 5.1.
#>

function Test+Expand-Dropped-Files {
    param(
        $DroppedFiles,
        [string]$Depth = "0",
        [string[]]$Include = $Null
  )

if ( $DroppedFiles.count -eq 0 ) {
    Write-Host "Supply directory and/or file names. Wildcards are allowed." -ForegroundColor Red
    Exit
    # Makes sure there is at least one parameter, or Get-ChildItem defaults to all in current directory.
    }

[bool]$err = $False
foreach ($f in $DroppedFiles) {
    if ( ( [string]::IsNullOrWhiteSpace($f) ) -or ( (Test-Path -Path $f) -eq $False ) ) {
    Write-host "ERROR--File or directory does not exist: " -NoNewline  -ForegroundColor Red
    Write-host $f
    $err = $True
    }
}
if ( $err ) { Exit }

$Droppedfiles = Get-ChildItem -Path $DroppedFiles -File -Recurse -Depth $Depth -Include $Include -ErrorAction Stop
# '-Depth 0' limits expansion of files in directories to the top level.
# '-Include $Null' will include everything.

Write-Output $DroppedFiles
}

I put the above in a file named "function_Test+Expand-Dropped-Files.ps1", dot-source this from a directory in my system path, and call the function from my working PowerShell script as follows:

param ( [Parameter(ValueFromRemainingArguments=$true)] $DroppedFiles )

. function_Test+Expand-Dropped-Files
$DroppedFiles = Test+Expand-Dropped-Files $DroppedFiles  -Depth 0 -Include $Null
# '-Depth 0' limits directory expansion to first level.
# '-Include' can specify a string array of specifications, such as: -Include *.doc,*.docx 
# '-Depth' and '-Include' are optional and default to 0 and $Null (includes all) respectively.

#Your specific code to process each file goes within the following loop:
$i = 0
foreach ($File in $DroppedFiles) {
    Write-Host "Processing:" $File.FullName
    $i++
}
Write-Output "$i file(s) processed.`n"

You can then drop files and/or directories on the following .bat file (give the .bat file the same base name as the working .ps1 script):

@echo off
set PSScript=%~dpn0.ps1
powershell.exe -NoLogo -NoExit -File "%PSScript%" %*

Alternatively you can drop files on a similar shortcut file as described in prior posts.

Another option to dot-sourcing the function as done above is to put the "Test+Expand-Dropped-Files" function in your PowerShell profile or set it up as a PowerShell Module.