Command prompt window opens and closes immediately while running powershell script

2.3k views Asked by At

I'm trying to run a powershell script as follows:

Start-Process -FilePath "C:\svn\Services\trunk\Services.In4m.Agent.Host\bin\agent.exe" -Argument --help

Any reason why this could be happening. Also, how to write a script so that the command prompt window does not close immediately?

2

There are 2 answers

3
Scott Kearney On

It looks like the program is ending. If the command just outputs the help text and ends, the window will close upon completion.

Your best bet will be to capture the output from the command and display it out to the user.

& "C:\svn\Services\trunk\Services.In4m.Agent.Host\bin\agent.exe" --help | Write-Host

If you want to open a separate window and wait for the user to hit enter to close it, you could do something like this:

Start-Process 
  -FilePath "powershell.exe" 
  -ArgumentList "& 'C:\svn\Services\trunk\Services.In4m.Agent.Host\bin\agent.exe' --help; Read-Host"

It just depends on what you're trying to achieve.

4
Syed. On

Add this at the end of your powershell script:
It asks user to press any key and waits until user presses a key.

Write-Host "Press any key to continue ...".
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")