如何从 shell 获取 windows 中的窗口标题

如何从 shell 获取 windows 中的窗口标题

我可以使用命令通过 Windows Shell 设置标题title some string ,但如何获取某些进程的标题?

我尝试了命令tasklist /v,但我的标题非常非常长,这就是为什么我只收到部分标题。另外,我在考虑使用 wmic 实用程序,但找不到所需的标志。

答案1

现在的 shell 是 Powershell:

Get-Process | Where-Object {$_.mainWindowTitle} | Format-Table Id, Name, mainWindowtitle -AutoSize

选定流程的标题为:

(Get-Process -id 8748 -ErrorAction SilentlyContinue).MainWindowTitle

答案2

下面的脚本显示tasklist /v /FO:CSV不会截断长窗口标题和独立解决两个任务:

  • 第1部分显示全部有用窗口标题以及csv格式中的图像名称和 PID;findstr用于将输出范围缩小到真的有用的标题。
  • 第2部分显示执行脚本的命令提示符窗口的 PID 和标题(我自己的cmd)。

脚本

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

rem Part 1: ALL USEFUL WINDOW TITLES
echo(
set "_myExcludes=^\"conhost ^\"dwm ^\"nvxdsync ^\"nvvsvc ^\"dllhost ^\"taskhostex"
for /F "tokens=1,2,8,* delims=," %%G in ('
  tasklist /V /fo:csv ^| findstr /V "%_myExcludes%"
                                        ') do (
    if NOT "%%~J"=="N/A" echo %%G,%%~H,%%J
)

rem Part 2: MY OWN cmd WINDOW TITLE
echo(
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!
ENDLOCAL

输出

==> TITLE ...but my title is very, very long, that's why I receive only partial title. Also 
I was thinking about wmic utility, but can't find desired flag!!!

==> .\SU\378790.bat

"Image Name",PID,"Window Title"
"VDeck.exe",4032,"VIA HD Audio Deck"
"chrome.exe",5760,"How to get window title in windows from shell - Super User - Google Chrom
e"
"powershell_ise.exe",5568,"Windows PowerShell ISE"
"cmd.exe",4980,"...but my title is very, very long, that's why I receive only partial title.
 Also I was thinking about wmic utility, but can't find desired flag!!! - .\SU\378790.bat"
"PSPad.exe",5108,"378790.bat"
"cmd.exe",3888,"d:\bat"
"cmd.exe",5648,"Administrator: Command Prompt"

_myCmdPIDno=4980
_myCmdTitle=...but my title is very, very long, that's why I receive only partial title. Als
o I was thinking about wmic utility, but can't find desired flag!!!

==>

资源(必读):

答案3

自动热键可以帮助您实现这一点。让我们编写一个脚本,将所有打开的窗口的进程和标题输出到标准输出

WinGet, windows, list

Loop, %windows%
{
    id := windows%A_Index%
    WinGet, process, ProcessName, ahk_id %id%
    WinGetTitle, title, ahk_id %id%
    FileAppend, %process% %title%`n, *
}

ExitApp

编译获取便携式的脚本。EXE文件

现在,我们可以从 Windows 命令行运行以下命令:

MyScript.exe | more

例子:

截屏

相关内容