How to execute a PowerShell script from XYplorer?

Discuss and share scripts and script files...
Post Reply
zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

How to execute a PowerShell script from XYplorer?

Post 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

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to execute a PowerShell script from XYplorer?

Post 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;
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

Re: How to execute a PowerShell script from XYplorer?

Post 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;

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to execute a PowerShell script from XYplorer?

Post 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
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

Re: How to execute a PowerShell script from XYplorer?

Post 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");

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to execute a PowerShell script from XYplorer?

Post 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...
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

Re: How to execute a PowerShell script from XYplorer?

Post 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;

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to execute a PowerShell script from XYplorer?

Post 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.
One of my scripts helped you out? Please donate via Paypal

XSreyfQT4hV6
Posts: 18
Joined: 19 Nov 2017 00:06

Re: How to execute a PowerShell script from XYplorer?

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

Post Reply