0

I have a script I wrote in PowerShell ISE that works perfectly. It even works if I copy and paste it into regular PowerShell. However when I run it with regular PowerShell by making it the default program to open it with, then double-clicking, it just opens, displays some red text, and closes.

Is there a way to stop it from closing so I can view the red text or is this a common issue that can be fixed easily?

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
Captor
  • 65
  • 3
  • 12
  • One easy way, and a good practice to use anyway, is to use the start-transcript command near the beginning of the script so that the output is logged to a file. – Tony Hinkle Apr 26 '16 at 17:56
  • .psq script should't be opened by PowerShell. You can just run it in PowerShell, by name, for example . .\script.ps1. – Alexan Apr 26 '16 at 17:58
  • Start transcript. I tossed it in there, gave no output. Made no file in normal PS but in ISE it did. So no help there. – Captor Apr 26 '16 at 18:01
  • Is there not a way to make a script run simply by double clicking it? – Captor Apr 26 '16 at 18:01
  • Tried that, explained that up top too :/ – Captor Apr 26 '16 at 18:03
  • 1
    http://stackoverflow.com/questions/16739322/how-to-keep-the-shell-window-open-after-running-a-powershell-script – Alexan Apr 26 '16 at 18:04
  • What is the output of get-executionpolicy? – Tony Hinkle Apr 26 '16 at 18:06
  • Unrestricted..and maybe let me look through it – Captor Apr 26 '16 at 18:07
  • No error if I powershell.exe -file xx. Does everything it should. – Captor Apr 26 '16 at 18:13
  • Post the script if you can. The easiest way for us to help you in this case is to repro the problem. – Tony Hinkle Apr 26 '16 at 18:15
  • Okay but it's 376 lines... – Captor Apr 26 '16 at 18:15
  • Have you tried using a try catch block around the entire script? In the catch block you can use a "start-sleep -s 90" so that you have some time to see it. – Tony Hinkle Apr 26 '16 at 18:17
  • Just did, no go. It's not even running as powershell console as default program – Captor Apr 26 '16 at 18:19
  • Is it a corporate PC that may have a Group Policy blocking it? – Tony Hinkle Apr 26 '16 at 18:21
  • It is a corporate machine, however a Co worker of mine that no longer works here. Created a script that functions perfectly with powershell as its default program but was created in ise – Captor Apr 26 '16 at 18:23
  • Can you right-click and select Open With and select Windows PowerShell? Whatever is happening sounds like a Windows shell issue, not a PowerShell issue per se. – Tony Hinkle Apr 26 '16 at 18:24
  • I've done that, no luck. That's why I posted tbh :/ – Captor Apr 26 '16 at 18:26
  • Sounds like something is really jacked up, or blocked. There may be a Group Policy that is applied to you that wasn't applied to your coworker. A possible workaround is just to create a .bat file with "powershell.exe -f C:\whatever\script.ps1" – Tony Hinkle Apr 26 '16 at 18:28
  • Yeah, that doesn't explain why I'm able to use his script that way though – Captor Apr 26 '16 at 18:32
  • Can you repeat this on other systems? I am curious enough now to see it. – Matt Apr 26 '16 at 18:36
  • _It's not even running as powershell console as default program_ What does this mean exactly? Are you saying ps1 extension does not run in PowerShell and that you have an association issue? – Matt Apr 26 '16 at 18:38
  • Alright, so. I had my co worker use the script. Right click, run with powershell. Works great. Then I had him close. And make powershell (x86) default, stopped working. Tried to use a version without x86 on end couldn't locate one so gave up on that but I'm using the one without and it's not working either. – Captor Apr 26 '16 at 18:47
  • Ok Captor you are describing an association issue. Why are you changing the defaults exactly? Was it not working before? Have a try at this: http://community.idera.com/forums/topic/restoring-windows-powershell-file-associations/ I suspect PowerShell is erroring out as something was overwritten that should not have been. If nothing else you can confirm this is not the issue. – Matt Apr 26 '16 at 18:50
  • I usually have the default program as ise as this is a work in progress. And I got to the point where I wanted it to be ran without a window, blah blah blah. Then I made it default as powershell console. And it didn't work, tried a few things to see if it'd qork nothing. Made note pad the default just now and hit run as powershell and it works... I guess I'm just gonna have to deal with running it as powershell rather than having the default program as powershell – Captor Apr 26 '16 at 19:01

2 Answers2

0

Try running from cmd prompt using:

powershell -noexit <path to the script> 

Additionally, I think the error script may be throwing could be due to execution policy on your machine blocking the script from executing. Otherwise transcript would work. Ensure that the execution policy is set appropriately using Set-ExecutionPolicy cmdlet.

Aman Sharma
  • 1,930
  • 1
  • 17
  • 31
  • Pretty sure it was established that it would work in this scenario without error. This does not explain the issue. – Matt Apr 26 '16 at 18:37
  • You are correct @Matt. I did not saw "Show 15 more comments" before. My bad. Still my response answers the original question. Isn't it. – Aman Sharma Apr 26 '16 at 18:39
  • I think the question is unclear right now which is why there is so many comments. This cannot be answered concretely until we learn more. If this is the right answer then it is surely a dupe but cannot be sure at this time either way. The OP changed the association which is something to wonder. – Matt Apr 26 '16 at 18:41
0

The practice of setting Powershell scripts to run when double clicked is highly discouraged for security reasons. Please ensure you are aware of the risks involved before you proceed.

You cannot simply 'make Powershell as default program' through the Windows Explorer interface. The Run with Powershell context menu option uses the following command:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"

The "safest" way (bearing in mind the above caveat) to do what you are trying to achieve is to use Default Programs Editor to set the Run with Powershell context menu option to be the default.

Xtremity
  • 101
  • 2