7

I am using PowerShell to start Chrome and then kill it. Start-Process gets the PID, but when I try to kill Chrome it says PID does not exist.

The script:

$myp = Start-Process "chrome.exe" -PassThru $mypid = $myp.Id Write-Host "PID = $mypid" Stop-Process -Id $mypid -Force 

And here's the output:

PS C:\users\mattw\Documents> ./a.ps1 PID = 15824 Stop-Process : Cannot find a process with the process identifier 15824. 
1
  • I used "Get-Process | Where-Object {$_.ProcessName -Like "chrome"} | Stop-Process -Force" and it works perfectly for my needs. Thank you! Commented Aug 3 at 16:44

2 Answers 2

10

The issue isn't that powershell isn't storing the PID, it is that chrome.exe is changing its PID and adding more.

This is the snippet you get when you google "google chrome PID changes"

Google Chrome is designed with a multi-process architecture, which means it utilizes multiple Process IDs (PIDs) for different components and functions. This architecture is a key reason why PIDs associated with Chrome can appear to change frequently or why multiple PIDs are observed for a single Chrome instance.

If you were to try the same for notepad.exe, then it would work.

enter image description here

If you want to close all chrome instances, you can do it like this

Get-Process | Where-Object {$_.ProcessName -Like "*chrome*"} | Stop-Process -Force 
0
6

Firstly, PowerShell does not store any PIDs anywhere. That's not a job for a scripting language, at all. It accesses them through Windows .NET APIs and they are of course only stored on the system/kernel level. They are only assigned as part of temporary objects when using certain functions, such as return value of Start-Process.

Secondly, keep in mind that processes can be very dynamic - they can quickly appear and disappear. The chrome.exe executable you're starting is just a launcher and/or process manager for Chrome. The window itself is a different process - each of windows has a separate one. Even each non-frozen tab can have own process.

Assuming you only run one window, the shortest way to kill it in PowerShell would be something like:

Get-Process chrome | Stop-Process -Force 

as the -Name param is optional and this matches all chrome.exes exactly.

Or even simplier:

Stop-Process -Name chrome -Force 

Otherwise you need to match it by e.g. window title.

3
  • 2
    > PowerShell does not store any PIDs anywhere ... er, no. The PID of the started process is stored as part of the Process object (literally. it's allocated an int32 in PowerShell's private working set and stored there.). It's generated by the OS but PS stores a copy of it. Commented Aug 5 at 8:41
  • That said you are correct that it initially retrieves it via the system handle, on first access. Commented Aug 5 at 8:44
  • I don't think you understood the point... Of course you can use anything like $randomPidVar = 666 and that would also store the started process ID. If you analyze the question logically, the OP is clearly showing some "stored" ID from the Process object yet is wondering why PowerShell isn't storing the ID. Hence the explantation that PowerShell isn't the one to blame for not storing process data, as it isn't its job, it just accesses it temporarily, doesn't matter if in object instances or anything else. Commented Aug 5 at 9:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.