1

So I just wrote my first PowerShell script that uses Windows Forms to create and display a GUI.

If I run the script in PowerShell ISE, or call the .ps1 from PowerShell command line directly, the script runs, the GUI appears and everything is fine. However, if I simply double-click on the .ps1 file to "launch" the script, the PowerShell prompt quickly opens and closes, and there's no GUI (it terminates immediately).

Am I missing something? Here's the last line in my script to make the GUI appear.

$crsc_main_form.ShowDialog()

Thank you.

2 Answers 2

1

I found out what the issue was. The error was caused by the location of my .ps1 file. It was in a folder that had spaces in it, so when I tried to launch it, the PowerShell prompt would open quickly and close, and the error message is showed for a millisecond was "Term $PATH isn't recognized as the name of an cmdlet".

The "solution" was to move the file in a location where there's no spaces in the path (ie: C:\Temp). I could probably tweak my system so that when launched, the path will be put inside " ".

Sign up to request clarification or add additional context in comments.

Comments

-1

Without seeing the surrounding code I'm just speculating here but some code cannot be run unless you set your execution policy to either bypass or unrestricted.

My guess is that when you are attempting to just run the .ps1 file as described the session is restricted

Try throwing in the following:

executionpolicy > $env:USERPROFILE\Desktop\executionpolicy.txt

This will write a text file to your desktop showing what the execution policy is

2 Comments

Unless a script was downloaded from the web, the RemoteSigned policy allows execution as well. If execution of a script is permitted in the ISE, it is also permitted from a PowerShell console window and from File Explorer. If the execution policy were the problem, placing a diagnostic command inside the script is of limited use, because it won't get to execute - check the execution policy in a PowerShell console window. Finally, the full name of the command you're referencing is Get-ExecutionPolicy. While omitting the Get- part works, doing so is obscure and slow.
It wasn't an issue with the ExecutionPolicy, since I'm able to run other PowerShell scripts (.ps1) directly by launching them. The error I had was because the file is located in a path that has spaces in it, and therefore returned an "Term $PATH isn't recognized as the name of an cmdlet".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.