如何启动基于控制台的流程并使用 Powershell 应用自定义标题
我正在将旧的 cmd
命令转换为 Powershell,当前使用:
START "My Title" Path/To/ConsoleApp.exe
这将按预期启动 ConsoleApp,并将“我的标题”作为窗口标题。它已被替换为 Start-Process,它可以正常工作,但不提供更改标题的机制。
是否有另一种方法可以在不使用 cmd
命令的情况下实现此目的?
I am converting an old cmd
command to Powershell, and currently use:
START "My Title" Path/To/ConsoleApp.exe
This works as expected to launch ConsoleApp with My Title as it's window title. This has been replaced with Start-Process which works correctly, but does not provide a mechanism to change the title.
Is there another way to do this without resorting to using the cmd
command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更改进程主窗口的文本时有一个小怪癖:如果您在启动进程后尝试立即更改文本,则可能会由于许多可能的原因之一而失败(例如,显示的控件的句柄)该文本在函数调用时不存在)。因此,解决方案是在尝试更改文本之前使用
WaitForInputIdle()
方法:请注意,在您进行自己的更改后,应用程序本身仍然可以更改窗口文本。
There is a small quirk when changing the text of the process' main window: if you try to change the text straight after you have started the process, it may fail due to one of many possible reasons (e.g. the handle to the control which displays the text does not exist at the time of the function call). So the solution is to use the
WaitForInputIdle()
method before trying to change the text:Be aware that the application itself can still change the window text after you have made your own change.
我用 cmd.exe 尝试过,效果很好。
I tried this with cmd.exe and it worked well.
如果您想使用 powershell 生成一个具有自定义标题的进程,请尝试:
注意转义标题字符串的反引号字符,它们至关重要!
If you want to spawn a process with powershell with a custom title try:
Note the backtick characters that escape the string for the title, they are vital!
$host.UI.RawUI.WindowTitle = "new title"
正如 George 所说,任何人/任何人都可以将其设置回来(例如自定义提示功能)。
$host.UI.RawUI.WindowTitle = "new title"
As already been said by George, anything/anyone can set it back (like custom prompt functions for example).