Search code examples
powershellbrave

I get wrong process id number after launch brave profile with Start-Process


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?


Solution

  • 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