When I run a program on PowerShell it opens a new window and before I can see the output, the window closes. How do I make it so PowerShell keeps this window open?
-
is this program an exe, or a batch file? Can you give the exact command you are running, perhaps changing the name of the executable? – Justin Dearing Feb 11 '12 at 21:46
-
It's an exe. I added the folder that the program is in to my path and when I type in the name of the program it runs, except the window it opens in closes too quickly for me to read what it says. The program is a program to test python programs. As for your comment below, I don't know how to wrap a program in a batch file :/ – Erman Feb 11 '12 at 21:56
-
I updated my answer to clarify how to do that. It also might make sense to redirect your programs output to a text file. – Justin Dearing Feb 11 '12 at 22:59
-
Related (duplicate): *[How to keep the shell window open after running a PowerShell script?](http://stackoverflow.com/questions/16739322)* – Peter Mortensen Jan 21 '16 at 15:38
7 Answers
Try doing:
start-process your.exe -NoNewWindow
Add a -Wait
too if needed.

- 290,304
- 63
- 469
- 417
-
2When I do this it tells me "Start-Process : This command cannot be executed due to the error: The system cannot find the file specified." Which is strange because I added the location of the exe to my path. – Erman Feb 11 '12 at 22:04
-
@Erman - Make sure it really is. Restart Powershell if needed. Or use the full path to see what is going on. – manojlds Feb 11 '12 at 22:07
-
-
When I added -NoNewWindow argument to my start-process, I got the same error as in comment 1 for Erman, but when I fully qualified the executable it resolved the error for me so `start-process 'full path to\your.exe' -NoNewWindow`, the command ran as expected. – Dan May 27 '16 at 16:40
-
1For my case, the `-Verb RunAs` parameter set was causing an AmbiguousParameterSet error – jrbe228 Jan 19 '23 at 02:51
The OP seemed satisfied with the answer, but it doesn't keep the new window open after executing the program, which is what he seemed to be asking (and the answer I was looking for). So, after some more research, I came up with:
Start-Process cmd "/c `"your.exe & pause `""

- 363
- 4
- 10
-
2An alternative for the PowerShell purists would be to use powershell in place of cmd: Start-Process powershell "your.exe ; read-host 'Press Enter to continue'" – Cary Mar 25 '13 at 18:50
-
2I use this: `Start-Process "powershell" -wait -ArgumentList "tree 'D:' ; pause"`. – larkee Jul 29 '15 at 15:39
I was solving a similar problem few weeks ago. If you don't want to use & (& '.\program.exe'
) then you can use start process and read the output by start process (where you read the output explicitly).
Just put this as separate PS1 file - for example (or to macro):
param (
$name,
$params
)
$process = New-Object System.Diagnostics.Process
$proInfo = New-Object System.Diagnostics.ProcessStartInfo
$proInfo.CreateNoWindow = $true
$proInfo.RedirectStandardOutput = $true
$proInfo.RedirectStandardError = $true
$proInfo.UseShellExecute = $false
$proInfo.FileName = $name
$proInfo.Arguments = $params
$process.StartInfo = $proInfo
#Register an Action for Error Output Data Received Event
Register-ObjectEvent -InputObject $process -EventName ErrorDataReceived -action {
foreach ($s in $EventArgs.data) { Write-Host $s -ForegroundColor Red }
} | Out-Null
#Register an Action for Standard Output Data Received Event
Register-ObjectEvent -InputObject $process -EventName OutputDataReceived -action {
foreach ($s in $EventArgs.data) { Write-Host $s -ForegroundColor Blue }
} | Out-Null
$process.Start() | Out-Null
$process.BeginOutputReadLine()
$process.BeginErrorReadLine()
$process.WaitForExit()
And then call it like:
.\startprocess.ps1 "c:\program.exe" "params"
You can also easily redirect output or implement some kind of timeout in case your application can freeze...

- 30,738
- 21
- 105
- 131

- 4,337
- 2
- 22
- 31
-
This is an interesting approach, and it works, except that the output created by the external process comes back out of order. Also, the script doesn't appear to wait for the process to end before continuing, as I see its output interspersed with the external command's output. – STLDev Jan 11 '22 at 16:22
If the program is a batch file (.cmd or .bat extension) being launched with cmd /c foo.cmd
command, simply change it to cmd /k foo.cmd
and the program executes, but the prompt stays open.
If the program is not a batch file, wrap it in a batch file and add the pause
command at the end of it. To wrap the program in a batch file, simply place the command in a text file and give it the .cmd extension. Then execute that instead of the exe.

- 14,270
- 22
- 88
- 161
With Startprocess and in the $arguments scriptblock, you can put a Read-Host
$arguments = {
"Get-process"
"Hello"
Read-Host "Wait for a key to be pressed"
}
Start-Process powershell -Verb runAs -ArgumentList $arguments

- 61
- 1
- 2
Just one more option to add the the list. You can add a "&& PAUSE" to the end so it runs this if the first command is successful.
e.g., Start-Process cmd "/c `"reg.exe query hkcu\printers\connections && pause`""

- 1