0

In cmd.exe you can list currently running windows by their PIDs and window titles using:

tasklist /fi "imagename eq cmd.exe" /fo list /v

However, this does not work if cmd is running within the Windows Terminal application. It lists the PIDs but not the titles, which show up as "N/A." Seeing as Windows 11 forces you to always use the terminal, this breaks a lot of batch applications that use tasklist to self-identify or to identify other apps.

Is there a way to get a list of currently open Windows Terminal tabs with their titles? Alternatively, is there a way to launch cmd.exe outside of the windows terminal on windows 11 without changing the default?

I've tried tasklist /fi "imagename eq WindowsTerminal.exe" /fo list /v but it only lists a single window without the titles of the tabs.

2
  • 1
    "Seeing as Windows 11 forces you to always use the terminal, this breaks a lot of batch applications that use tasklist to self-identify or to identify other apps." - This is actually a feature you can disable.
    – Ramhound
    Apr 5, 2023 at 15:12
  • Windows Terminal is a super powerful tool, however, so it'd be nice if another workaround existed.
    – Mark Deven
    Apr 5, 2023 at 15:17

2 Answers 2

0

New Launch Method

This method launches any program and gets the PID, and ideally should be the standard moving forward:

powershell -executionPolicy bypass -command "& {$process = start-process $args[0] -passthru -argumentlist $args[1..($args.length-1)]; exit $process.id}" FileToRun.bat Parameters
echo Process ID of new process: %errorlevel%

Self-Identify PID in Batch file

While this does not let you identify the PID of other Batch Files, this method allows a batch file to self identify its PID:

FOR /F "tokens=*" %%G in ('powershell -ExecutionPolicy Unrestricted  -Command "(New-Guid).guid"') do Set my_guid=%%~G
powershell -ExecutionPolicy Unrestricted  -Command "(Get-WmiObject Win32_Process -Filter ProcessId=$PID).ParentProcessId>"%temp%\%my_guid%.pid"
FOR /F "tokens=*" %%P in ('type "%temp%\%my_guid%.pid"') do set my_pid=%%~P
del /f /q "%temp%\%my_guid%.pid"

(Note that a temp file must be used because when the command is run in a for loop, the for loop becomes the parent process).

Launch Batch File in Traditional Conhost.exe

You can also launch cmd.exe windows in the traditional window, which will be backward compatible with other methods.

cmd.exe was not the original emulator that Windows Terminal replaces. cmd.exe is a command processor that runs in the emulator. The traditional emulator was conhost.exe.

As such you can run a batch file or cmd.exe directly in conhost:

conhost.exe Listener.bat

You can launch a batch file in the background with a VBScript as follows:

Set oShell = CreateObject("WScript.Shell") 
oShell.Run """Listener.bat""", 0 

This still runs it in the traditional conhost.exe instance and works with tasklist.

Bug Report to Microsoft

I did add also add a Problem Report to the feedback hub "Windows Terminal Blocks tasklist from seeing cmd.exe window titles [Breaks Batch Files]" enter image description here

0

Using PowerShell you can get the IDs this way:

Get-Process -Name "cmd" | Select Id

Assuming you want to list only cmd tabs. Works for me for Windows Terminal processes in Windows 11. Windows titles seem to be empty though, likely because they are subprocesses that normally don't work as separate windows sunce they're tabs.

3
  • Yeah that's the same as what tasklist /fi "imagename eq cmd.exe" /fo list /v does. The issue is that there's no way to get the PID of say, a batch file you just launched unless you have the title or some way of knowing.
    – Mark Deven
    Apr 7, 2023 at 16:38
  • Not really the same and you said that one doesn't work for you at all, unless it's classic inaccurate "it doesn't work". I think you try to fix the problem from the wrong side and the question should have been "How to get a pid of the launched bat?" or something like that, depending on what you're trying to do exactly.
    – Destroy666
    Apr 7, 2023 at 17:34
  • I should have included the output of the tasklist command. I've updated that. To some extent, yes I am trying to get the PID of a launched batch file, however seeing as the title solution is already implemented in many scripts, I was looking specifically for a way to update this method and keep on using it. An entirely new method would be much less helpful, but I suppose effective. I'll start a new question for that.
    – Mark Deven
    Apr 8, 2023 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .