Note that there are some explanatory texts on larger screens.

plurals
  1. PODrag and Drop to a Powershell script
    text
    copied!<p>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.</p> <p>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.</p> <p><em>Test Script</em></p> <pre><code>#Test.ps1 param ( [string[]] $Paths, [string] $ExampleParameter ) "Paths" $Paths "args" $args </code></pre> <p><strong>Attempt #1</strong></p> <p>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.</p> <p><em>Shortcut for Attempt #1</em></p> <pre><code>powershell.exe -noprofile -noexit -file c:\Test.ps1 </code></pre> <p><strong>Wrapper Script</strong></p> <p>I found that I can do this with a wrapper script...</p> <pre><code>#TestWrapper.ps1 &amp; .\Test.ps1 -Paths $args </code></pre> <p><em>Shortcut for Wrapper Script</em></p> <pre><code>powershell.exe -noprofile -noexit -file c:\TestWrapper.ps1 </code></pre> <p><strong>Batch File Wrapper Script</strong></p> <p>And it works through a batch file wrapper script...</p> <pre><code>REM TestWrapper.bat SET args='%1' :More SHIFT IF '%1' == '' GOTO Done SET args=%args%,'%1' GOTO More :Done Powershell.exe -noprofile -noexit -command "&amp; {c:\test.ps1 %args%}" </code></pre> <p><strong>Attempted Answers</strong></p> <p>Keith Hill made the excellent <a href="https://stackoverflow.com/questions/2819908/drag-and-drop-to-a-powershell-script/2820207#2820207">suggestion</a> 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.</p> <pre><code>powershell.exe -noprofile -noexit -command "&amp; {c:\test1.ps1 $args}" </code></pre> <p>Has anyone found a way to do this without the extra script?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload