Reputation: 4485
I got some strange behaviour when executing a powershell script.
powershell.exe -noexit
./myscript.ps1
, myscript works just fine.When I double-click myscript however, powershell opens for some milliseconds, I see that it shows some error (red font) and the powershell window closes. I'm unable to track down the error causing this problem since the powershell windows closes to fast.
I even tried one single big try-catch block around my hole script, catching any [Exception]
and writing it down to a log file. However: the log file is not generated (catch is not called).
How can I track that issue? What could possibly be causing the trouble? Please note that my execution-policy is set to unrestricted.
Upvotes: 6
Views: 7124
Reputation: 2667
Following Ronen's lead, I wrote a quick C# console app to fix the bug in PowerShell that it cannot launch from a double-click in Explorer if there's a space anywhere in the full path. Once you compile the console app with the code below, put the exe somewhere in C:\ProgramFiles for example, and then right click on any powershell script in explorer, choose open with, choose another app, more apps, scroll to the bottom, look for another app on this pc, and then locate the console app exe you just created. Now any time you double-click on a script with this extension, it should work. Do for each type of powershell script extension that you want to double-click to run. Damn you to hell Microsoft.
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace PowershellLauncher
{
internal class Program
{
static void Main(string[] args)
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(args[0]));
StringBuilder shortPath = new StringBuilder(255);
GetShortPathName(args[0], shortPath, shortPath.Capacity);
Process.Start($"C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe", shortPath.ToString());
}
// https://www.c-sharpcorner.com/article/convert-long-to-short-file-names-in-C-Sharp/
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortPath,
int shortPathLength
);
}
}
Upvotes: 0
Reputation: 21
I also wasn’t able to run a script by double clicking it although running it manually worked without a problem. I have found out that the problem was in the path. When I ran a script from a path that contained spaces, such as:
C:\Users\john doe\Documents\Sample.ps1
The scipt failed to run. Moving the script to:
C:\Scripts\Sample.ps1
Which has no spaces, solved the problem.
Upvotes: 2
Reputation: 6185
I was in exactly the same situation as described in the question : my script worked everywhere except when double-clicking.* When I double-clicked a powershell windows would open but then it will close after a second or so. My execution-policy is also set to unrestricted.
I tried the selected answer concerning FType Microsoft.PowerShellScript.1
but it didn't change anything.
The only solution I found was a work around: create a bat file which start the powershell.
powershell.exe -File "C:\Users\user\script\myscript.ps1"
.bat
Double-click the bat
.ahk
to start my powershell with a shorcut and it didn't work when pointing directly to the powershell. I had to point to the .bat
Upvotes: 0
Reputation: 487
If you would catch the error you will most likely see this
The file cannot be loaded. The file is not digitally signed. The script will not execute on the system. Please see "Get-Help about_signing" for more details.
Because you are able to run it from the shell you started yourself, and not with the right mouse button click "Run With PowerShell", I bet you have x64 system. Manually you are starting the one version of PowerShell where execution policy is configured, while with the right click the other version of the PowerShell is started.
Try to start both version x64 and x86 version and check for security policies in each
Get-ExecutionPolicy
Upvotes: 0
Reputation: 155
This is most likely an issue with your local Execution Policy.
By default, Powershell is configured to NOT run scripts that are unsigned (even local ones). If you've not signed your scripts, then changing your default double-click 'action' in Windows will have no effect - Powershell will open, read the execution policy, check the script's signature, and finding none, will abort with an error.
In Powershell:
Help about_execution_policies
gives you all the gory details, as well as ways to allow unsigned scripts to run (within reason - you'd probably not want to run remote ones, only ones you've saved onto the system).
EDIT: I see at the tail end of your question that you've set Execution Policy to 'unrestricted' which SHOULD allow the script to run. However, this might be useful info for others running into execution policy issues.
Upvotes: 0
Reputation: 42035
Before trying the suggestion invoke this to see your current settings (if you want restore them later):
cmd /c FType Microsoft.PowerShellScript.1
Then invoke this (note that you will change how your scripts are invoked "from explorer" by this):
cmd /c @"
FType Microsoft.PowerShellScript.1=$PSHOME\powershell.exe -NoExit . "'%1'" %*
"@
Then double-click the script, it should not exit, -NoExit
does the trick. See your error messages and solve the problems.
But now all your scripts invoked "from explorer" keep their console opened. You may then
remove -NoExit
from the above command and run it again or restore your
original settings.
Some details and one good way to invoke scripts in PS v2 is here. Unfortunately it is broken in PS v3 - submitted issue.
Upvotes: 6
Reputation: 26130
by default, for security reason when you double clic on a .ps1 file the action is : Edit file, not Run file .
to execute your script : right-click on it and choose run with powershell
Upvotes: 2