Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell how to get the ParentProcessID by the ProcessID

Tags:

powershell

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?

like image 779
Pascal Avatar asked Nov 25 '15 08:11

Pascal


2 Answers

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
like image 86
Mathias R. Jessen Avatar answered Oct 19 '22 04:10

Mathias R. Jessen


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.

like image 39
Pascal Avatar answered Oct 19 '22 05:10

Pascal