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.
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.
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 Use
tasklist /fi "windowtitle eq <Title of window*>"
For example:
tasklist /fi "windowtitle eq Notepad*"
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.
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"
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 *
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
© 2022 - 2024 — McMap. All rights reserved.