2

I need to close all windows of a specific Chrome profile. Suppose I execute:

chrome.exe -remote-debugging-port=4000 --user-data-dir=F://chrome

in cmd/bat to open a new Chrome profile. I need to close this specific Chrome profile from cmd/bat.

I have tried this:

taskkill /IM chrome.exe -remote-debugging-port=4000 --user-data-dir=F://chrome

but it's not working.

1 Answer 1

0

You can accomplish this using the batch file below:

for /f "tokens=3 delims=," %%a in ('wmic process where "caption='chrome.exe'" get processID^,commandline /format:csv ^| FIND "-remote-debugging-port=4000 --user-data-dir=F://chrome"') do set _pid=%%a
taskkill /f /pid %_pid% /t

Explanation

  • wmic process where "caption='chrome.exe'" get processID^,commandline /format:csv lists all chrome.exe processes with the command line used to launch them and the process ID that we will use to kill it.
  • Piping (|) this into FIND "-remote-debugging-port=4000 --user-data-dir=F://chrome" will return only the chrome.exe process started with the command you used.
  • for /f "tokens=3 delims=," %%a in ("...") do set _pid=%%a will set the _pid environment variable to the process ID of the process we want to kill.

    The wmic command combined with the find command should only give us a single result. If you used the command multiple times, then this will only find the process ID of the last process in the list.

  • taskkill /f /pid %_pid% /t:

    • /f forces the task to end, because otherwise Chrome will remain open.
    • /pid %_pid% will supply the process ID we got in the for loop.
    • /t tells taskkill to kill the entire process tree.

      Since every Chrome tab and extension runs in its own process, we want to make sure these get killed as well.

7
  • not working ..its not closing that specific browser that was opened May 30, 2019 at 15:52
  • What do you see if you just run wmic process where "caption='chrome.exe'" get processID^,commandline /format:csv | FIND "-remote-debugging-port=4000 --user-data-dir=F://chrome" in the console? Try removing -remote-debugging-port=4000 from the command and just use the --user-data-dir portion.
    – Worthwelle
    May 30, 2019 at 15:57
  • cmd execute so fast and close itself not letting me to see anything in the console.tried removing -remote-debugging-port=4000 ..still same..not working May 30, 2019 at 17:29
  • Try opening the command prompt manually and then running the script or adding PAUSE at the end. That will keep the command window open.
    – Worthwelle
    May 30, 2019 at 17:30
  • @Mahbubshaun If you run the for command directly in the console, you have to change %%a to %a.
    – Worthwelle
    May 30, 2019 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.