Get a list of all open windows from the command line
Asked Answered
D

6

28

In Windows 7, is it possible to obtain a list of all open desktop windows from the command line? I know that it's possible to obtain a list of all running processes from the command line, but I want to know if it's possible to obtain a list of open windows as well.

Duiker answered 19/3, 2013 at 4:5 Comment(3)
This solution appears similar, but I'm not sure if it does exactly the same thing. Is obtaining a list of all running Windows applications equivalent to obtaining a list of all open windows?Duiker
Here's one possible solution: #18456591Duiker
there is a solution: #25771886Hagar
D
9

The "/v" option lists the window names in the last column. As in "tasklist /v". You can also pipe it into another application or find to do filtering.

Devondevona answered 13/6, 2018 at 16:41 Comment(2)
tasklist /v is super slowAcoustic
And tasklist /v fails to query many titles, for instance when you have a lot of chrome tabs open, it only lists the title of the chrome tab that is in front.Insecticide
P
7

Use

tasklist /fi "windowtitle eq <Title of window*>"

For example:

tasklist /fi "windowtitle eq Notepad*"
Purington answered 7/5, 2014 at 10:58 Comment(4)
Now I wonder if it would be feasible to obtain the list of open windows without knowing the title of each window in advance.Duiker
There should be a eq or ne condition in there as well: tasklist /fi "windowtitle eq Notepad*"Proprietary
@Proprietary - You should have edited the answer to fix that. I've taken care of it.Marela
I don't need active processes, I need active windows. One process can open few windows. Also this command doesn't work even if only one window is created.Griffie
W
3

WinLister from NirSoft list all windows active on a machine as well as associated information (title, path, handle, class, position, process ID, thread ID, etc.). It has a GUI interface rather than command-line.

Wolof answered 22/1, 2019 at 19:15 Comment(1)
better then is GUIPropView from nirsoft.Moraceous
I
1

if you just use the following command, it will list all active processes

tasklist

Or filtering by session name would restrict a bit more to get processes started by console:

tasklist /FI "SESSIONNAME eq Console"

Inference answered 22/4, 2015 at 13:18 Comment(1)
I don't need active processes, I need active windows. One process can open few windows.Griffie
C
1

Use powershell. The command is: Get-Process

You can try this:

##Method 1: (Gives you all the processes)
Get-Process

## Method 2: Detailed Info On a specific named Process
$ProcessTerm="chrome"
#Run This:
$FindProcess = Get-Process | Where-Object {$_.MainWindowTitle -like "*$processterm*"}
Get-Process -ID $FindProcess.ID | Select-Object *

# FindProcess.ID will give you the ID of the above process

#Method 3: (if you know the process ID)
$ProcessID = "9068"
$FindProcess = Get-Process | Where-Object {$_.id -eq "$ProcessID"}
Get-Process -ID $FindProcess.Id | Select-Object *
Chlorite answered 9/2, 2019 at 17:22 Comment(1)
The question was about windows and not about processes.Gulosity
E
0

I had to dig around a lot, but this C# implementation in PowerShell seems to do the trick. I don't know C# very well, so this is a pure copy-and-paste solution. It can very well be improved upon ;)
(I.e. filter out nameless windows already in C# and do all the DllImports in one go.)

This solution also seems to get the Tray Icon "windows" (I believe).

Add-Type -TypeDefinition @"
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Runtime.InteropServices;
  using System.Text;

  public class Window {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("user32.dll")]
    static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    public static List<Dictionary<string, string>> GetOpenWindows() {
      var windows = new List<Dictionary<string, string>>();

      EnumWindows((hWnd, lParam) => {
        if (IsWindowVisible(hWnd)) {
          var title = new StringBuilder(256);
          GetWindowText(hWnd, title, 256);

          uint processId;
          GetWindowThreadProcessId(hWnd, out processId);
          var process = Process.GetProcessById((int)processId);

          var window = new Dictionary<string, string>
          {
            { "ProcessName", process.ProcessName },
            { "ProcessID", process.Id.ToString() },
            { "WindowsTitle", title.ToString() }
          };

          windows.Add(window);
        }
        return true;
      }, IntPtr.Zero);

      return windows;
    }
  }
"@

[array]$Windows = @()

[Window]::GetOpenWindows() | where WindowsTitle | #has a name
  foreach {
    $item = New-Object PSCustomObject | select ProcessName, ProcessID, WindowsTitle
    $item.ProcessName = $_.ProcessName
    $Item.ProcessId = $_.ProcessID
    $item.WindowsTitle = $_.WindowsTitle

    $Windows += $item
  }

$Windows

Evince answered 13/5 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.