How to gently close Chrome using CMD or VBS?

11,745

Solution 1

you can use powershell to close all windows with chrome as process name. It won't kill the task but gently close all chrome browser windows, like if you clicked on top-right cross. Here is the command line to put in batch :

powershell -command "Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"

Solution 2

Refer to the answer of @tuxy42 : You can write with vbscript to open chrome browser like this : Open_Chrome_Browser.vbs

Set ws = CreateObject("Wscript.Shell")
siteA = "https://stackoverflow.com/questions/62516355/google-chrome-always-says-google-chrome-was-not-shut-down-properly"
ws.Run "chrome -url " & siteA

And if you want to close it safely : safely_close_chrome_browser.vbs

Set ws = CreateObject("Wscript.Shell")
psCommand = "Cmd /C powershell -command ""Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"""
ws.run psCommand,0,True
Share:
11,745
Francisco Rojas
Author by

Francisco Rojas

Updated on June 07, 2022

Comments

  • Francisco Rojas
    Francisco Rojas almost 2 years

    I'm having a little problem about closing gently Chrome in order to automatically clean cache.

    I have a website that is not refreshing correctly, but it does if I clean the cache.

    For now I have a .bat file with the following code:

    taskkill /F /IM chrome.exe /T > nul
    del /q "C:\Users\Francisco\AppData\Local\Google\Chrome\User Data\Default\Cache\*"
    FOR /D %%p IN ("C:\Users\Francisco\AppData\Local\Google\Chrome\User Data\Default\Cache\*.*") DO rmdir "%%p" /s /q
    timeout 8
    start chrome --restore-last-session --start-fullscreen
    

    Now, I know that taskkill /F forces the process to close and that works but as soon as Chrome opens, it shows a message that Chrome wasn't closed correctly and asks me if I want to restore the last session.

    If i remove the /f option, Chrome doesn't close:

    ERROR: the process with PID 8580 (PID secondary process 8896) Could not finish.
    Reason: This process can be terminated only forcibly (with the / F option).

    So... I need one of two options;

    1. Is there a way to gently close Chrome like pressing Alt+F4 using CMD or a VBS?2. Is there a way to hide that Chrome restore last session message?

    I'd prefer the first option since it's cleaner.