Reputation: 1305
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?
Upvotes: 19
Views: 34369
Reputation: 2279
PowerShell (Core 6 at least) have CodeProperty Parent
on Process
.
[System.Diagnostics.Process]::GetCurrentProcess().Parent
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
53 128,45 68,27 144,38 102672 1 WindowsTerminal
Upvotes: 0
Reputation: 11048
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"
Upvotes: 1
Reputation: 1566
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
Upvotes: 14
Reputation: 1305
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.
Upvotes: 15
Reputation: 174435
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
Upvotes: 22