You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell列出含窗口标题的进程?SSH及VS窗口问题咨询

问题解答

1. 如何使用PowerShell列出所有带有窗口标题的进程?

直接使用以下命令即可,它会过滤出包含非空窗口标题的进程,并按指定列格式化输出:

Get-Process | Where-Object {$_.MainWindowTitle} | Format-Table Id, Name, MainWindowTitle -AutoSize
  • Get-Process:获取系统中所有正在运行的进程
  • Where-Object {$_.MainWindowTitle}:筛选掉无窗口标题的后台进程
  • Format-Table -AutoSize:按进程ID、名称、窗口标题三列自动调整宽度输出

2. 通过SSH登录后,能否以其他用户身份列出这些进程?

默认情况下不行。Windows中,带可视化窗口的进程与用户的交互式桌面会话绑定,SSH登录属于非交互式会话,无法直接访问其他用户会话中的窗口进程。如果要实现,需要满足两个条件:

  • 拥有管理员权限
  • 使用工具(如PsExec)在目标用户的桌面会话中启动PowerShell,或通过远程桌面登录到目标用户会话后执行命令

3. 为什么Get-Process只列出一个Visual Studio窗口,wmctrl也无法查看所有窗口?

关于Get-Process的问题

Get-Process的MainWindowTitle是进程级属性,仅返回该进程的主窗口标题。Visual Studio(devenv.exe)采用单进程多窗口架构,所有打开的项目窗口都是主进程的子窗口,不会被Get-Process单独识别,因此只会显示主窗口的标题。

关于wmctrl的问题

wmctrl是针对X Window系统的工具,若在WSL/Cygwin环境下用于查看Windows的VS窗口,可能存在以下问题:

  • VS窗口未通过X forwarding映射到当前X服务器,无法被捕获
  • VS的部分子窗口无顶级窗口属性或标题为空,被wmctrl自动过滤
  • 跨用户会话的窗口无法被wmctrl识别

如果要获取VS的所有窗口,可以通过调用Windows API枚举窗口实现,示例PowerShell代码如下:

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class WindowLister {
    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
    [DllImport("user32.dll")]
    public static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
    [DllImport("user32.dll")]
    public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count);
    [DllImport("user32.dll")]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
}
"@

$windows = @()
$devenvPid = (Get-Process devenv).Id
$null = [WindowLister]::EnumWindows({
    param($hWnd, $lParam)
    $sb = New-Object System.Text.StringBuilder(256)
    $null = [WindowLister]::GetWindowText($hWnd, $sb, 256)
    $title = $sb.ToString()
    if (-not [string]::IsNullOrEmpty($title)) {
        $null = [WindowLister]::GetWindowThreadProcessId($hWnd, [ref]$pid)
        if ($pid -eq $devenvPid) {
            $windows += [PSCustomObject]@{
                ProcessId = $pid
                WindowTitle = $title
                WindowHandle = $hWnd
            }
        }
    }
    return $true
}, [IntPtr]::Zero)

$windows | Format-Table ProcessId, WindowTitle -AutoSize

这段代码会枚举所有带标题的窗口,并筛选出属于devenv进程的所有窗口条目。

内容的提问来源于stack exchange,提问作者Jason Hunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 06:20:52