Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

how does PowerShell integrate with Windows Explorer?

Closed Thread
 
Thread Tools Display Modes
Old 01-21-2008   #1 (permalink)
Arne Schween
Guest
 
Posts: n/a

how does PowerShell integrate with Windows Explorer?

Hi,

PowerShell script files are not supposed to be opened by the explorer for
security reasons.

So, what's the recommended way, say when I use Windows Explorer to
find/select/collect some files and want these files to be processed by some
PowerShell script?

I'd like to have something like a droplet or "send to" context menu entry
and wrote a script containing a foreach($arg in $args) loop for processing
the files. The script works perfectly when invoked from the PowerShell
prompt.
But when I try to execute something like "powershell -command scriptName
arg1 arg2..". I run into trouble with spaces in path and file names.
Whatever I try using curly braces or qoting I end up either having all the
args stored as one arg, having all the args splitted at the space character
positions, or seeing PowerShell complaining about unbalanced braces/quotes.

Or do I am on the wrong track? Should I go for having Windows Explorer
sending an object stream of System.IO.FileInfo somehow?

...any help is appreciated.

Arne

 
Old 01-22-2008   #2 (permalink)
Jacob Saaby Nielsen
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

Hey Arne,

my guess is that if you do a Send As in the registry yourself, with "powershell.exe"
%1, that'd work.

All of the arguments go into one argument array called $args. In your script
you can access that array
with $args[0], $args[1], and so forth, all the way up to $args.length (which
is the number of elements
in the array).

Also, the command you want to execute should be something more like:

"powershell" -command "scriptname". If you include -command between "", it
will be included as part of
the path name.

I think you're basically on the right track here, but have a look at "params"
and "$args array". There's no
reason why what you're trying to do, shouldn't work.

Best Regards,
Jacob Saaby Nielsen

http://www.pipforhelvede.net
gmail: jacob DOT saaby
hotmail (IM/LinkedIN/Facebook): same as gmail
Quote:

> Hi,
>
> PowerShell script files are not supposed to be opened by the explorer
> for security reasons.
>
> So, what's the recommended way, say when I use Windows Explorer to
> find/select/collect some files and want these files to be processed by
> some PowerShell script?
>
> I'd like to have something like a droplet or "send to" context menu
> entry
> and wrote a script containing a foreach($arg in $args) loop for
> processing
> the files. The script works perfectly when invoked from the PowerShell
> prompt.
> But when I try to execute something like "powershell -command
> scriptName
> arg1 arg2..". I run into trouble with spaces in path and file names.
> Whatever I try using curly braces or qoting I end up either having all
> the
> args stored as one arg, having all the args splitted at the space
> character
> positions, or seeing PowerShell complaining about unbalanced
> braces/quotes.
> Or do I am on the wrong track? Should I go for having Windows Explorer
> sending an object stream of System.IO.FileInfo somehow?
>
> ..any help is appreciated.
>
> Arne
>

 
Old 01-22-2008   #3 (permalink)
Arne.Schween
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

On 22 Jan., 08:51, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
wrote:
Quote:

> my guess is that if you do a Send As in the registry yourself, with "powershell.exe"
> %1, that'd work.
Thanks Jacob,

but configured that way Windows Explorer starts an individual
PowerShell instance for every object selected in Windows explorer.
Also the parameters will be splitted at space character positions. The
$args array is filled with the parts. I tried to place quotes around
%1, but to no avail. Of course one could rebuild the complete path and
filename from the elements of the $args array, but this sounds like a
hack, and starting potentially very many PowerShell instances might
seriously impact system performance.

The basic problem seems to be the way PowerShell.exe parses its
commandline. It looks like PowerShell builds the scripts $args array
from the concatenation of its own arguments, thus loosing all quoting
around parameters containing spaces.

BTW I'm using V2 CTP, does 1.0 behave any different?

Arne
 
Old 01-22-2008   #4 (permalink)
Jacob Saaby Nielsen
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

Hey Arne,

it's completely normal for the arguments to be split at the space character.
It's the way powershell interprets arguments.

Can you provide an example of what it is you're seeing in the $args array,
and
what it is specifically you're trying to pass to the script ?

I don't know if 2.0 ctp behaves differently from 1.0. I use 1.0, and am not
moving
to 2.0 until there's a pretty solid release candidate.

Best Regards,
Jacob Saaby Nielsen

http://www.pipforhelvede.net
gmail: jacob DOT saaby
hotmail (IM/LinkedIN/Facebook): same as gmail
Quote:

> On 22 Jan., 08:51, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
> wrote:
>
Quote:

>> my guess is that if you do a Send As in the registry yourself, with
>> "powershell.exe" %1, that'd work.
>>
> Thanks Jacob,
>
> but configured that way Windows Explorer starts an individual
> PowerShell instance for every object selected in Windows explorer.
> Also the parameters will be splitted at space character positions. The
> $args array is filled with the parts. I tried to place quotes around
> %1, but to no avail. Of course one could rebuild the complete path and
> filename from the elements of the $args array, but this sounds like a
> hack, and starting potentially very many PowerShell instances might
> seriously impact system performance.
>
> The basic problem seems to be the way PowerShell.exe parses its
> commandline. It looks like PowerShell builds the scripts $args array
> from the concatenation of its own arguments, thus loosing all quoting
> around parameters containing spaces.
>
> BTW I'm using V2 CTP, does 1.0 behave any different?
>
> Arne
>

 
Old 01-22-2008   #5 (permalink)
Shay Levi
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

Try replacing "%1" with "%L". I used it once but can't remember if it meant
to support long file name or names
that contain spaces.

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> On 22 Jan., 08:51, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
> wrote:
>
Quote:

>> my guess is that if you do a Send As in the registry yourself, with
>> "powershell.exe" %1, that'd work.
>>
> Thanks Jacob,
>
> but configured that way Windows Explorer starts an individual
> PowerShell instance for every object selected in Windows explorer.
> Also the parameters will be splitted at space character positions. The
> $args array is filled with the parts. I tried to place quotes around
> %1, but to no avail. Of course one could rebuild the complete path and
> filename from the elements of the $args array, but this sounds like a
> hack, and starting potentially very many PowerShell instances might
> seriously impact system performance.
>
> The basic problem seems to be the way PowerShell.exe parses its
> commandline. It looks like PowerShell builds the scripts $args array
> from the concatenation of its own arguments, thus loosing all quoting
> around parameters containing spaces.
>
> BTW I'm using V2 CTP, does 1.0 behave any different?
>
> Arne
>

 
Old 01-22-2008   #6 (permalink)
Arne.Schween
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

On 22 Jan., 11:55, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
wrote:
Quote:

> Hey Arne,
>
> it's completely normal for the arguments to be split at the space character.
> It's the way powershell interprets arguments.
>
> Can you provide an example of what it is you're seeing in the $args array,
> and
> what it is specifically you're trying to pass to the script ?
Hi Jacob,

just as simple as:

place a testArguments.ps1 files in the search path. the script
testArguments.ps1 just echoes its parameters line by line:

# testArguments.ps1
foreach ($arg in $args) {
echo $arg
}

open up powershell and type

testArguments "1 2" 3

you'll get

1 2
3

So "1 2" was interpreted as one argument as it should.
Then try some of the valid variants to start powershell with a
commandline from Windows Explorer, f.x.

powershell.exe -noexit -command testArguments "1 2" 3

I get

1
2
3

That's my problem.

Arne
 
Old 01-22-2008   #7 (permalink)
Jacob Saaby Nielsen
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

Hey Arne,

I found this on techtarget.com, perhaps you should try %* instead ?

% (variable)

A variable is a replaceable parameter. The parameters %0 and %1 to %9 can
be placed anywhere within a batch file. When the batch file is run, %0 is
replaced by the name of the batch file, and the argument variables %1 to
%9 are replaced by the corresponding parameters entered on the command line.

For example, to copy the contents of one folder to another, you would add
the following statement in your batch file:

xcopy %1\*.* %2

When you run the file, you would type the following:

mybatch.bat C:\afolder D:\bfolder.

The effect is the same as if you had written xcopy C:\afolder \*.* D:\bfolder
in the batch file.

The % parameter expands the batch script argument variables (%0, %1, ...,
%9) as follows:

%* in a batch script is a wildcard reference to all the arguments. For individual
argument variables, the expansion options are explained in the following
tables.

Variable Description
%~1 expands %1 and removes any surrounding quotes (")
%~f1 expands %1 to a fully qualified path name
%~d1 expands %1 to a drive letter
%~p1 expands %1 to a path
%~n1 expands %1 to a file name
%~x1 expands %1 to a file extension
%~s1 expanded path contains short names only
%~a1 expands %1 to file attributes
%~t1 expands %1 to date/time of file
%~z1 expands %1 to size of file
%~$PATH:1 searches the directories listed in the PATH environment variable
and expands %1 to the fully qualified name of the first one found. If the
environment variable name is not defined or the file is not found by the
search, then this modifier expands to the empty string.

The modifiers can be combined to get compound results:

Variable Description
%~dp1 expands %1 to a drive letter and path
%~nx1 expands %1 to a file name and extension
%~dp$PATH:1 searches the directories listed in the PATH environment variable
for %1 and expands to the drive letter and path of the first one found
%~ftza1 expands %1 to a dir-like output line

In the above examples %1 and PATH can be replaced by other valid values.
The %~ syntax must be terminated by a valid argument number. The %~ modifiers
may not be used with %*.

Best Regards,
Jacob Saaby Nielsen

http://www.pipforhelvede.net
gmail: jacob DOT saaby
hotmail (IM/LinkedIN/Facebook): same as gmail
Quote:

> On 22 Jan., 11:55, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
> wrote:
>
Quote:

>> Hey Arne,
>>
>> it's completely normal for the arguments to be split at the space
>> character. It's the way powershell interprets arguments.
>>
>> Can you provide an example of what it is you're seeing in the $args
>> array,
>> and
>> what it is specifically you're trying to pass to the script ?
> Hi Jacob,
>
> just as simple as:
>
> place a testArguments.ps1 files in the search path. the script
> testArguments.ps1 just echoes its parameters line by line:
>
> # testArguments.ps1
> foreach ($arg in $args) {
> echo $arg
> }
> open up powershell and type
>
> testArguments "1 2" 3
>
> you'll get
>
> 1 2
> 3
> So "1 2" was interpreted as one argument as it should.
> Then try some of the valid variants to start powershell with a
> commandline from Windows Explorer, f.x.
> powershell.exe -noexit -command testArguments "1 2" 3
>
> I get
>
> 1
> 2
> 3
> That's my problem.
>
> Arne
>

 
Old 01-22-2008   #8 (permalink)
Arne.Schween
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

On 22 Jan., 12:23, Shay Levi <n...@xxxxxx> wrote:
Quote:

> Try replacing "%1" with "%L". I used it once but can't remember if it meant
> to support long file name or names
> that contain spaces.
Thanks for the hint. But the parameters are still split at space
character positions.

Arne
 
Old 01-22-2008   #9 (permalink)
Shay Levi
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

Arne

By the time the command gets to the specified script, PowerShell already
consumed the first two quotes.
Try adding two more, as in:

# Start > Run ...
powershell.exe -noexit -command c:\testArguments """1 2""" 3


Or wrap them with single quotes:

powershell.exe -noexit -command c:\testArguments '"1 2"' 3


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> On 22 Jan., 11:55, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
> wrote:
>
Quote:

>> Hey Arne,
>>
>> it's completely normal for the arguments to be split at the space
>> character. It's the way powershell interprets arguments.
>>
>> Can you provide an example of what it is you're seeing in the $args
>> array,
>> and
>> what it is specifically you're trying to pass to the script ?
> Hi Jacob,
>
> just as simple as:
>
> place a testArguments.ps1 files in the search path. the script
> testArguments.ps1 just echoes its parameters line by line:
>
> # testArguments.ps1
> foreach ($arg in $args) {
> echo $arg
> }
> open up powershell and type
>
> testArguments "1 2" 3
>
> you'll get
>
> 1 2
> 3
> So "1 2" was interpreted as one argument as it should.
> Then try some of the valid variants to start powershell with a
> commandline from Windows Explorer, f.x.
> powershell.exe -noexit -command testArguments "1 2" 3
>
> I get
>
> 1
> 2
> 3
> That's my problem.
>
> Arne
>

 
Old 01-22-2008   #10 (permalink)
Arne.Schween
Guest
 
Posts: n/a

Re: how does PowerShell integrate with Windows Explorer?

On 22 Jan., 13:33, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
wrote:
Quote:

> Hey Arne,
>
> I found this on techtarget.com, perhaps you should try %* instead ?
Hi Jacob,

cool features but it looks like only cmd.exe supports this. When
extending the context menu via registry entries, these strings are not
expanded at all. But, in a batch file executed by cmd.exe it does
work.

I'll write a small C# adapter application that will collect the
command line parameters and call RunspaceInvoke.Invoke to execute the
script with the correct parameters. I'll report if that does work.

Arne





 
 
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
4 the powershell team integrate powershell with onenote pghboemike PowerShell 2 04-20-2008 07:31 PM
How to integrate 'Windows Live Mail' and 'Windows Contacts?' John E. Golden Live Mail 2 03-10-2008 02:13 PM
any way for PowerShell and Windows Workflow Foundation to integrate? Zhao, Q. Alex PowerShell 0 07-20-2006 01:02 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49