简体   繁体   English

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

[英]How to get PowerShell to keep a command window open?

When I run a program on PowerShell it opens a new window and before I can see the output, the window closes.当我在 PowerShell 上运行程序时,它会打开一个新窗口,在我看到输出之前,窗口关闭。 How do I make it so PowerShell keeps this window open?如何使 PowerShell 保持此窗口打开?

Try doing:尝试做:

start-process your.exe -NoNewWindow

Add a -Wait too if needed.如果需要,也添加-Wait

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). OP 似乎对答案感到满意,但在执行程序后它并没有保持新窗口打开,这就是他似乎在问的问题(也是我正在寻找的答案)。 So, after some more research, I came up with:所以,经过一些更多的研究,我想出了:

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

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...您还可以轻松地重定向输出或实现某种超时,以防您的应用程序冻结...

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.如果程序是使用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 pause command at the end of it.如果程序不是批处理文件,则将其包装在批处理文件中并在其末尾添加pause命令。 To wrap the program in a batch file, simply place the command in a text file and give it the .cmd extension.要将程序包装在批处理文件中,只需将命令放在文本文件中并为其指定 .cmd 扩展名。 Then execute that instead of the exe.然后执行它而不是exe。

With Startprocess and in the $arguments scriptblock, you can put a Read-Host使用 Startprocess 并在 $arguments 脚本块中,您可以放置​​一个读取主机

$arguments = {
    "Get-process"
    "Hello"
    Read-Host "Wait for a key to be pressed"
  }
Start-Process powershell -Verb runAs -ArgumentList $arguments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM