windows 如何让 PowerShell 保持命令窗口打开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9244280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:12:51  来源:igfitidea点击:

How to get PowerShell to keep a command window open?

windowsshellpowershell

提问by Erman

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?

当我在 PowerShell 上运行程序时,它会打开一个新窗口,在我看到输出之前,窗口关闭。如何使 PowerShell 保持此窗口打开?

回答by manojlds

Try doing:

尝试做:

start-process your.exe -NoNewWindow

Add a -Waittoo if needed.

-Wait如果需要,也添加一个。

回答by Cary

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:

OP 似乎对答案感到满意,但在执行程序后它并没有保持新窗口打开,这就是他似乎在问的问题(也是我正在寻找的答案)。所以,经过一些更多的研究,我想出了:

Start-Process cmd "/c `"your.exe & pause `""

回答by Tomas Panik

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).

几周前我正在解决类似的问题。如果您不想使用 & ( & '.\program.exe') 那么您可以使用 start process 并通过 start process 读取输出(您显式读取输出)。

Just put this as separate PS1 file - for example (or to macro):

只需将其作为单独的 PS1 文件 - 例如(或宏):

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...

您还可以轻松地重定向输出或实现某种超时,以防您的应用程序冻结...

回答by Justin Dearing

If the program is a batch file (.cmd or .bat extension) being launched with cmd /c foo.cmdcommand, simply change it to cmd /k foo.cmdand the program executes, but the prompt stays open.

如果程序是使用cmd /c foo.cmd命令启动的批处理文件(.cmd 或 .bat 扩展名),只需将其更改为cmd /k foo.cmd并且程序执行,但提示保持打开状态。

If the program is not a batch file, wrap it in a batch file and add the pausecommand 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.

如果程序不是批处理文件,则将其包装在批处理文件中并pause在其末尾添加命令。要将程序包装在批处理文件中,只需将命令放在文本文件中并为其指定 .cmd 扩展名。然后执行它而不是exe。