Page 1 of 1

How to execute a PowerShell script from XYplorer?

Posted: 08 Dec 2016 22:32
by zBernie
Hello, I'm brand new to XYplorer. So far I'm loving it!

Can someone please tell me how I can execute a PowerShell script from XY? Maybe from a user button? I have a .ps1 script which simply outputs "hello world" to a file. This is how I've been trying:


$scriptPath = self("path");
$cmd = "Powershell -NoExit -File "C:\Users\ra-bartonbf\PowerShell\HelloWorldFile.ps1"";
msg($cmd);
run($cmd);


-Thanks

Re: How to execute a PowerShell script from XYplorer?

Posted: 08 Dec 2016 23:28
by highend
Welcome to the forum.

The quoting is wrong.

Code: Select all

    $cmd = """Powershell"" -NoExit -File ""C:\Users\ra-bartonbf\PowerShell\HelloWorldFile.ps1""";
    run $cmd;

Re: How to execute a PowerShell script from XYplorer?

Posted: 09 Dec 2016 05:51
by zBernie
Within a user defined button, the $cmd syntax did not work. However, this runret command does work. Is this syntax acceptable to use?

-Thanks

runret("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\Users\bernie\PowerShell\test.ps1");


//$cmd = """Powershell"" -NoExit -File ""C:\Users\bernie\PowerShell\test.ps1""";
//run $cmd;

Re: How to execute a PowerShell script from XYplorer?

Posted: 09 Dec 2016 07:52
by highend
It does work from an CTB (but you didn't copy the indentation from within the code tags correctly).

In your first example you wanted the powershell to stay open to see the command output while now
you want to hide it completely?

Is your new command syntactically correct? Yeah. But recommended? Not really.

- The powershell directory is in the path anyway. No need to use the full path to call it
- Much more important: It breaks in the same second your script name or path contains a space

Re: How to execute a PowerShell script from XYplorer?

Posted: 09 Dec 2016 18:41
by zBernie
Using the run command does not work for me. I've tried various permutations of the $cmd = line, to no avail. The runret command does work. I can't see what I am doing wrong.

-Thanks


$cmd = """powershell.exe"" -NoExit -File ""C:\Users\ra-bartonbf\PowerShell\HelloWorldFile.ps1""";
run $cmd;


runret("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\Users\ra-bartonbf\PowerShell\HelloWorld.ps1");

Re: How to execute a PowerShell script from XYplorer?

Posted: 09 Dec 2016 18:55
by highend
I can't see what I am doing wrong.
Neither can I if you don't show what exactly you're doing? You paste the two codelines. Fine. Where? In the "On left click" field directly? After pressing the "Edit..." button? The single line edit field isn't prepared (obviously) for multiple lines...

Re: How to execute a PowerShell script from XYplorer?

Posted: 10 Dec 2016 06:47
by zBernie
I'm simply pasting the two lines below into the "Try Script" dialog. It doesn't work for some reason. I know the HelloWorldFile.ps1 script works, and outputs text to a file. This seems like it should be straightforward.

$cmd = """powershell.exe"" -NoExit -File ""C:\Users\ra-bartonbf\PowerShell\HelloWorldFile.ps1""";
run $cmd;

Re: How to execute a PowerShell script from XYplorer?

Posted: 10 Dec 2016 07:44
by highend
Let me quote myself:
but you didn't copy the indentation from within the code tags correctly
From the help file:
In a multi-line script all lines apart from the first line have to be indented by at least one space.

Re: How to execute a PowerShell script from XYplorer?

Posted: 20 Dec 2017 17:36
by XSreyfQT4hV6
For anyone reading this later here is what i did.

Code: Select all

foreach($token, <selitems |>, "|" ) {
 $cmd = """powershell.exe"" -ExecutionPolicy Bypass -File ""$token""";
 run $cmd;
 }
This will run any selected powershell scripts bypassing script signing.
USE AT YOUR OWN RISK

This was my first script so if there is anything to improve let me know.
I put an icon on my toolbar that runs it.