7

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?

1
  • 2
    "parent process" is correct terminology, but i don't think "terminus" is... Mar 17, 2010 at 7:33

4 Answers 4

6

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.

4
  • I haven't tried this, but this sounds correct. Upvoted.
    – Alex
    Mar 18, 2010 at 4:59
  • (gwmi win32_process | ? processid -eq $pid).parentprocessid ($pid contains the current process id)
    – user33139
    Aug 21, 2013 at 23:29
  • @LukeSampson Why get all processes and then filter when WMI can do the filtering for you at source? (Ie. use the variable in the -filter parameter of Get-WmiObject (gwmi) rather than a separate Where-Object (?).)
    – Richard
    Aug 22, 2013 at 7:09
  • 1
    @Richard because I'm lazy and don't want to learn about a special parameter? Anyway thanks for your answer, it helped me :)
    – user33139
    Aug 22, 2013 at 14:13
1

This isn't from within cmd.exe itself but Process Explorer has a tree view which shows parent processes.

alt text

2
  • 2
    Yes, this is certainly correct, but I hoped there would be some command or something that I could type into cmd.exe. Mar 16, 2010 at 9:41
  • 1
    another SysInternals tool, pslist.exe, can also provide a tree view. unfortunately PPID (parent process ID) is not part of the general output, but if the cmd window was started by another application the tree view should show that relationship. technet.microsoft.com/en-us/sysinternals/bb896682.aspx Mar 17, 2010 at 7:32
0

Determine the process ID (pid) of the process you are curious about (i.e., the cmd.exe process) and type

wmic process where (ProcessId=<processID>) get Caption,ProcessId,ParentProcessID
This gets the same information (by, basically, the same mechanism) as Richard's answer, but using only cmd (i.e., not requiring PowerShell).

1
  • 1
    Can you elaborate on this a litle more?
    – Toto
    Dec 27, 2021 at 15:48
0

Reading the question closely, the OP asks

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?

This is different from the title, which should probably should have read "How can I determine my current cmd.exe's parent process"

Answering the question not the title, a very simple approach is to use AUTORUN.CMD. Since this run before every invocation of CMD, it can be as simple as:

SET /A LEVEL=%LEVEL+1
ECHO LEVEL = %LEVEL%

Caution AUTORUN.CMD, while it works as documented, it is defined per user and so is really only useful for non-critical things. Any other code which relies on AUTORUN.CMD, must handle the case where it does not exist.

For example, I use AUTORUN.CMD to append a star to the prompt, for each cmd nesting level:

PROMPT=%PROMPT%*$S

This is seen below.

Since CMD variables are local to the instance, when you exit cmd, the environment values from the "outer" cmd instance are used as can be seen here:

Microsoft Windows [Version 10.0.19044.1826]
(c) Microsoft Corporation. All rights reserved.
level = 1
T:\ * cmd
Microsoft Windows [Version 10.0.19044.1826]
(c) Microsoft Corporation. All rights reserved.
level = 2
T:\ * * exit
T:\ * echo %LEVEL%
1
T:\ *

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.