Windows – How to Determine cmd.exe’s Parent Process

command lineterminalwindows

Sometimes I find myself in a cmd.exe environment that itself was started by another cmd.exe or by another console-based application. Now, working in such an environment, I'd like to know what happens if I type exit, that is, if the cmd.exe window will disappear, or if it goes back to the cmd.exe or application that invoked it. This, of course, because sometimes as I work in cmd.exe I forget about how I called it.

So, is there a way to find out the parent process (if this is the correct term) of a cmd.exe within another cmd.exe?

Best Answer

You can use WMI to get this information. The Win32_Process class contains ParentProcessId

So (using PowerShell to execute WMI commands—other WMI tools are available):

gwmi Win32_Process -filter 'processid = 1234' | select ParentProcessId

will give the parent process id of process 1234.

Related Question