1

I want to open some brave profiles and close them later. So I want to save process id's on a file and iterate them to close it. But Id returned from Start-Process is not a real Id and I can't find that process later.

I checked on command prompt and found this: $process = Start-Process $bravePath -ArgumentList "--profile-directory=""$profile""" -PassThru $process.Id will give me 3060. But if I run this: Get-Process -Id 3060, I get this error: Get-Process : Not found process with identifier 3060.

I checked this Answer: https://stackoverflow.com/a/12084492/4599564 and it looks like brave is creating a new sub process on the collection of already existent brave process and closing the new one.

Is there any method to get the Id of the created brave process so I can stop it later?

2
  • stop-process -name brave
    – js2010
    Commented Jul 26, 2024 at 15:29
  • This will close also other brave process that I already have opened before this profile. Commented Jul 27, 2024 at 22:43

2 Answers 2

1

To get the Id of the last process created use this:

Get-Process | sort 'StartTime' -ErrorAction SilentlyContinue | select -Last 1 -Expand 'id'
2
  • Thank you very much for your answer. In effect this method give me the last process created, but it doesn't work for closing the brave process that i opened before. I'm not sure which process id I'm getting. Commented Jul 26, 2024 at 14:58
  • I have been playing around with your idea and found the solution. Thank you very much. Commented Jul 26, 2024 at 15:16
0

Thanks to @Someone answer, I could find a solution.

It looks like Brave creates several process for each open profile. If I run this:

Start-Process $bravePath -ArgumentList "--profile-directory=""$profile""" -PassThru
Get-Process | Where-Object { $_.ProcessName -like "*brave*" }

I get this list of new brave process:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    350      33    14080      66320      10,20   4492   1 brave
    171      11     2092       7680       0,00   6272   1 brave
    272      21    16832      42988       0,05  10540   1 brave
    299      23    25248      58416       0,16  10688   1 brave
    345      29   128892     167432       1,11  15372   1 brave
    306      23    12052      33520       0,13  27112   1 brave
   1350      57   178412     319076       2,09  29208   1 brave
    237      20    12644      27560       0,00  31348   1 brave
    213      15     8316      20500       0,03  38556   1 brave

Then I had an idea: Sort them by startime and stop the first process and it worked!

$braveProcessId=Get-Process | Where-Object { $_.ProcessName -like "*brave*" } | sort 'StartTime' -ErrorAction SilentlyContinue | select -First 1 -Expand 'id'
Stop-Process $braveProcessId-Force

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.