15

I've problems to get the ParentProcessID from a Process where I have the ProcessID. I tried it like this, this is how it works with the ProcessID:

$p = Get-Process firefox
$p.Id

But if I try it with the ParentProcessID, it doesn't work:

$p.ParentProcessId

Is there a way to get the ParentProcessID by the ProcessID?

Pascal
  • 1,255
  • 5
  • 20
  • 44

4 Answers4

20

As mentioned in the comments, the objects returned from Get-Process (System.Diagnostics.Process) doesn't contain the parent process ID.

To get that, you'll need to retrieve an instance of the Win32_Process class:

PS C:\> $ParentProcessIds = Get-CimInstance -Class Win32_Process -Filter "Name = 'firefox.exe'"
PS C:\> $ParentProcessIds[0].ParentProcessId
3816
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
15

This worked for me:

$p = Get-Process firefox
$parent = (gwmi win32_process | ? processid -eq  $p.Id).parentprocessid
$parent

The output is the following:

1596

And 1596 is the matching ParentProcessID I've checked it with the ProcessExplorer.

Pascal
  • 1,255
  • 5
  • 20
  • 44
  • The equivalent for Powershell Core is `(Get-CimInstance CIM_Process | ? processid -eq $p.id).parentProcessId` (as Get-WmiObject is deprecated since PowerShell 3 according to https://stackoverflow.com/a/54508009/727345). – JonoB Jul 11 '22 at 13:04
13

In PowerShell Core, the Process object returned by Get-Process cmdlet contains a Parent property which gives you the corresponding Process object for the parent process.

Example:

> $p = Get-Process firefox
> $p.Parent.Id
Rene Hernandez
  • 1,546
  • 13
  • 20
  • 2
    Note that this requires (elevated) administrator privileges. Otherwise it silently fails and the `Parent` member will be `$null`. The [WMI solution](https://stackoverflow.com/a/33912191/7571258) works with less privileges. – zett42 Nov 11 '21 at 11:11
  • 1
    @zett42 I don't think this is true anymore, it's working for me on a PowerShell without administrative privileges. – Vopel Apr 13 '23 at 22:35
2

I wanted to get the PPID of the current running PS process, rather than for another process looked up by name. The following worked for me going back to PS v2. (I didn't test v1...)

$PPID = (gwmi win32_process -Filter "processid='$PID'").ParentProcessId
Write-Host "PID: $PID"
Write-Host "PPID: $PPID"
BuvinJ
  • 10,221
  • 5
  • 83
  • 96