0
clear
$name = Read-host -Prompt 'Name of service'
$p = Get-process $name

$id = $p.Id
$parentId = $p.Parent.Id
echo $parentId

I've tried this, but it doesn't return anything

  • 1
    [ParentProcessID](https://stackoverflow.com/a/33912278/10106356) - you can refer this already answered post. – Karthick Ganesan Jun 04 '20 at 08:14
  • @KarthickGanesan I've already tried it, but no matter what process I introduce, it always gives me the same number –  Jun 04 '20 at 08:16
  • 1
    Does this answer your question? [Powershell how to get the ParentProcessID by the ProcessID](https://stackoverflow.com/questions/33911332/powershell-how-to-get-the-parentprocessid-by-the-processid) – Christian.K Jun 04 '20 at 08:23
  • @Jorge - That logically translates to the child processes being triggered by the same Parent process. If you think it is not true, please share more details or sample output. You can also use 'Process Explorer' to validate your results. – Karthick Ganesan Jun 04 '20 at 08:32

1 Answers1

0

Get-Process doesn't contain parent ID, so you need to use Get-CimInstance

clear
$name = Read-host -Prompt 'Name of service'

$ParentProcessIds = Get-CimInstance -Class Win32_Process -Filter "Name = '$name'"
$output = $ParentProcessIds[0].ParentProcessId

Write-Host $output

Powershell how to get the ParentProcessID by the ProcessID

Salve
  • 145
  • 1
  • 10
  • Doesn't work for me: `It cannot be indexed in a null matrix.` –  Jun 04 '20 at 08:27
  • That's the error you get when your $name doesn't contain a valid process name. Try only run the two lines: `$ParentProcessIds = Get-CimInstance -Class Win32_Process -Filter "Name = 'PROCESSNAME'" $ParentProcessIds[0].ParentProcessId` And hard-code the process name to make sure that it can find it. For example start CMD and write "conhost.exe" as the process – Salve Jun 04 '20 at 08:36