Google-chrome – How to kill all headless Chrome instances from the command line on Windows

command linegoogle-chrome

On Mac and Linux you can run

pkill -f "(chrome)?(--headless)"

to kill all headless Chrome instances.

I'd like something similar that will run on Windows.

Best Answer

  • Unfortunately, CommandLine is not easily retrievable through the normal process management commands, so we need to dip into WMI. This tends to make the command more complex, but you can alias it or hide it in a script/function you can reuse.

    Here's an example of how you can do this in PowerShell:

    Get-CimInstance Win32_Process -Filter "Name = 'chrome.exe' AND CommandLine LIKE '%--headless%'" | %{Stop-Process $_.ProcessId}
    

    Generalising it a bit, we can also get:

    $name = 'chrome.exe'
    $cmdcontains '--headless'
    Get-CimInstance Win32_Process -Filter "Name = '$name' AND CommandLine LIKE '%$cmdcontains%'" | %{Stop-Process $_.ProcessId}
    

    Alternatively, Name can use a looser matching, or we can filter purely on the CommandLine like you're currently doing:

    Get-CimInstance Win32_Process -Filter "CommandLine LIKE '%chrome.exe%--headless%'" | %{Stop-Process $_.ProcessId}
    

    More generally, you can extend it to a cmdlet via script: https://technet.microsoft.com/en-us/library/ff677563.aspx


    The same process applies if you want to do this in cmd, except you'd need to grab the PIDs from the output of wmic and pass it into taskkill.