3

I have made a very simple Powershell script with WinForms GUI. Everything works as intended but, when I run the .ps1 script with PowerShell a black empty console window appears at first and then the GUI shows.

Anyway to make the console window dissapear?

Best regards

3
  • How do you start your script? Commented May 19, 2012 at 14:53
  • Also: do you want window to disappear at some point (pretty easy to do) or don't appear at all (never had need to find a way and honestly, don't know if it even exists). Commented May 19, 2012 at 15:41
  • I am assuming that you are double cliking on the PS1 script. Is that correct? Also, sample code would really help in giving an accurate answer. Commented May 19, 2012 at 18:02

5 Answers 5

2

I wrote a small article on this subject (sorry in french) one year ago.

Here is the common solution using a small VBS script to start PowerShell hidding his window (the trick is in the last ,0).

Set Args = Wscript.Arguments
'MsgBox  "Chemin LDAP: " & Args(0)
'MsgBox  "Classe: " & Args(1)

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0

I also embeded PowerShell in an executable with no console called slxPShell2.EXE.

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

Comments

1

I found the above didn't work for me. I used this:

Set objShell = CreateObject("WScript.Shell")

objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -file " & "YourScript.ps1", 0, False

Set objShell = Nothing

Hope that helps.

Comments

1

This solution Minimizes Powershell window after it starts. Powershell window opens, then disapears, without using any outside code. Put at beginning of your script.

$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)

1 Comment

Note that if you want to show the window again later, you need to save its handle while it is still visible. So I do $hWindow = (Get-Process -id $pid).MainWindowHandle (also, shortened the code to get current process a little) only once early in my script, and pass that variable as the first ShowWindow parameter.
0

This is how I got this working:

  1. Have the Winforms GUI script in one ScriptOne.ps1 file
  2. Create another LaunchScriptOne.ps1 file with the content:

powershell.exe -WindowStyle Hidden -File "C:\path\to\ScriptOne.ps1".

The solution was provided in another thread on the same topic: Hide or Minimize the powershell prompt after Winform Launch

I hope someone will find a way to put this into one single script as well. The answers above in this thread did not help me, but maybe I did something wrong, idk.

Comments

0

I'm nube so no rep so can't comment inline... though wrt @Ipse's solution which I'm a fan of, I also make sure to close the hidden window when the script is done... not sure if PS gets around to this sort of auto-garbage collection, but suspect it's good best practice.

eg. at end of your script I'd suggest doing:

stop-process -Id $PID

(which should terminate that hidden window v. just leave it lurking around and tying up those resources).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.