Say I have several conhost processes that happen to be running already. Now I run a batch file, which starts a cmd process which, in turn, causes another conhost process to open up. From within the batch file

My goal is to be able to close down all the other conhost processes that were running previously (or possibly after the batch file was run) and not close the conhost associated with the very batch file and cmd.exe doing all the closing. If that conhost gets closed, so will its cmd process and the batch file will end prematurely (I want it to do other things after that).

This is not a duplicate of the question asked in the link above. That question only dealt with getting the pid of the cmd, not the associated conhost PID.


The following code snippet could lead to solution:

SETLOCAL EnableExtensions
::: get my own process ID - use any method
::: applied here: altered TonyRoth's answer https://serverfault.com/a/126643/257436
set "_title=a885974x%random%"
title %_title%
for /f "tokens=2" %%G in ('tasklist /V ^| findstr "%_title%"') do (
    set "_myProcessID=%%~G"

::: get the associated conhost process ID
set "_wQuery=ParentProcessId=%_myProcessID% and Name='conhost.exe'"
::: debug ::: wmic process where "%_wQuery%" get Name, ProcessId, WindowsVersion
for /f "usebackq" %%G in (`
  wmic process where "%_wQuery%" get ProcessId^, WindowsVersion^|findstr /R "[0-9]"
`) do set "_myConhostID=%%~G"

::: propagate results
set _my
  • Excellent! That's exactly what I was looking (and asking) for. You seem to be the only one who understands that I was asking mainly for the second part - getting the associated conhost PID. Now I just need to understand how it works. I'm really a novice about this stuff. – timothyjb Dec 8 '17 at 14:22
  • You are welcome. Next, consider asking at stackoverflow.com site. Read both serverfault.com/help/on-topic and stackoverflow.com/help/on-topic, please. – JosefZ Dec 8 '17 at 14:47

You can use a Microsoft tool called Process Explorer to find the active processes. You can find the process by dragging the target symbol over the windows you want the get the PID.

enter image description here

  • 1
    That's a manual process and the purpose of the batch script is to kill all the other conhost.exe's automatically. I need the batch script to be able to figure out what its conhost PID is, not me. – timothyjb Nov 30 '17 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.