How get the *original* cmd.exe console window title in PowerShell?

时间:2016-08-31 18:30:46

标签: powershell

As soon as PowerShell start run in a cmd.exe window the title of the window is changed to "Windows PowerShell"; however, the original title is stored in some place, because when PowerShell terminates, the original window title is recovered.

I spent some time reviewing the related documentation, but most examples show how get the current window title or change it using $host.ui.rawui.WindowTitle, but there is not a single example on getting the original cmd.exe window title (perhaps I just don't know what terms include in this search).

Is there any way to get the original console window title before PowerShell start run?

1 个答案:

答案 0 :(得分:0)

使用下一个代码段获取原始cmd控制台窗口标题:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

set "_myTitleTail= - %~0"
for /F "tokens=1,2,8,* delims=," %%G in ('
  tasklist /V /fo:csv ^| findstr /I /C:"%_myTitleTail%"
                                        ') do (
    set "_myTitleBatch=%%~J"
    set "_myCmdPIDno=%%~H"
)
call set "_myCmdTitle=%%_myTitleBatch:%_myTitleTail%=%%"
echo _myCmdPIDno=%_myCmdPIDno%
SETLOCAL EnableDelayedExpansion
  echo _myCmdTitle=!_myCmdTitle!
  rem powershell -NoExit -Command $Host.UI.RawUI.WindowTitle='"!_myCmdTitle!"'
ENDLOCAL

不幸的是,powershell -NoExit -Command $Host.UI.RawUI.WindowTitle='"!_myCmdTitle!"'(在上面的代码段中注释)解决方法仅适用于不包含powershell敏感字符(如撇号,双引号等)的简单标题。 例如,下一个组合适用于未注释的powershell行:

==> title some forgotten string
==> 39256629.bat

另一方面,在this (BTW, your current question inspired my answer there)

类似的情况下,我没有找到适当的转义
==> TITLE ...but my title is complex, that's why I can't find proper escaping!!!
==> 39256629.bat

Powershell会运行,但会一直抱怨绑定...点的意外令牌  或'撇号用于​​不同的逃避尝试......

相关问题