Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

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

#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:est.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:est1.ps1 $args}"

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_ROOTMicrosoft.PowerShellScript.1ShellExDropHandler
(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_ROOTMicrosoft.PowerShellScript.1ShellOpenCommand
(Default) = "C:WindowsSystem32WindowsPowerShellv1.0powershell.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_ROOTMicrosoft.PowerShellScript.1ShellExDropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

[HKEY_CLASSES_ROOTMicrosoft.PowerShellScript.1ShellOpenCommand]
@=""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:Windowssystem32. 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_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.ps1UserChoice which will override the default handling.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...