Close programs from the command line (Windows)

50

19

What is the proper way to close/exit programs from command line, similar to pressing the "X" close button in the corner of the window?

Im trying to close chrome under 3 different versions of windows: win7 ultimate, win7 home, winXP

Under Ultimate and XP: TSKILL chrome

Under Home: TASKKILL /IM chrome.exe


TSKILL chrome:

(It closes chrome, no cmd errors,
but chrome error restore when relaunch it)


TASKKILL /IM chrome.exe:

(It closes chrome, no chrome errors when relaunch it,
but errors in cmd: "impossible to terminate child processes(about 4-5), only by force with /F")


Should I ignore cmd child errors if on relaunch chrome show me no errors?

neoDev

Posted 2014-03-11T20:25:46.077

Reputation: 964

@MosheKatz: Can you show me some example of exit code please? I would like to know what happens when I hit the X button.. – neoDev – 2014-03-11T21:01:50.957

6Taskkill sends WM_CLOSE message to the target program unless /F (forced termination) is specified, to allow the program to have chances to clean up itself. Test this with Notepad - open a notepad and type something there, and enter "taskkill /im notepad.exe". You will see a prompt to save the data. – Scott Rhee – 2014-03-11T21:01:57.990

@ScottRhee: I already tried to run it without /F, but seems to be an unproper close... Am I wrong? – neoDev – 2014-03-11T21:07:17.517

@ScottRhee: Im trying with Chrome too! Sometimes, when I relaunch it says "restore tabs" in a yellow line under the URL similar to this: http://backroom.bostonproductions.com/wp-content/uploads/2012/07/image1.png

– neoDev – 2014-03-11T21:17:45.100

@Roger That's strange as I don't get that message with Taskkill /IM chrome.exe. Anyway, I believe you did the right thing. I wrote an answer below so please try that instead. – Scott Rhee – 2014-03-11T21:32:22.080

If taskkill works the same as TaskManager (which I haven't verified, but strongly suspect), then it works by first sending WM_CLOSE, and setting a timer (something like 100-200ms) after which TerminateProcess is called if the process still exists. That allows a responsive application (usually) to exit gracefully, and will blast a non-responsive application out of existence anyway. If Chrome shows an improper shutdown dialog on next start, it probably takes too long to shut down. – Damon – 2014-03-12T10:44:46.220

Answers

45

The proper way to close/exit a program ultimately depends upon the software. However, generally the best practice is for Windows programs to close whenever they receive the WM_CLOSE message. Properly releasing memory and closing handles. There are other messages that can signal the close of the application, but it is up to the author of the software how each message is handled.

taskkill /IM process.exe

taskkill sends the WM_CLOSE message and it is then up to the application whether to properly close. You may also want to use the /T option to also signal child processes.

Only use the /F option if you want to force the termination of the process.

Other options would include sending the Alt+F4 keys, using PowerShell, or 3rd party applications.

Update to Updated Question

Ignore, the errors. Chrome generates many processes. The errors are caused when an process does not acknowledge the WM_CLOSE message that TASKKILL sends. Only processes with a message loop will be able to receive the message, therefore, the processes that do not have a message loop will generate that error. Most likely, these processes are the chrome extensions and plugins.

To hide the errors capture the output

taskkill /IM chrome.exe >nul

Summary: TASKKILL is the proper way via command line to close applications per its WM_CLOSE implementation and the Microsoft KB that I linked.

David Ruhmann

Posted 2014-03-11T20:25:46.077

Reputation: 1 199

I updated my question, please have a look at it – neoDev – 2014-03-11T23:17:07.490

@Roger updated my answer. – David Ruhmann – 2014-03-11T23:46:17.730

what does it mean >nul? – neoDev – 2014-03-11T23:46:21.073

10> is the redirect operator for the cmd.exe and nul is the void of space where things go to never be heard from again. – David Ruhmann – 2014-03-11T23:48:06.850

9

Try NirCmd (Download: x86 x64)

It has "closeprocess" command which is designed to close processes gracefully. As per its document, it does not terminate apps but sends WM_CLOSE to all top-level windows of the target process.

usage example:

nircmd closeprocess chrome.exe

If this doesn't work, I bet your application has an unusual cleanup procedure. I would like to see what happens inside so please let me know what your target application is.

Scott Rhee

Posted 2014-03-11T20:25:46.077

Reputation: 1 923

I found this to work better: nircmd win close title "My process title" – driedler – 2019-08-23T13:20:17.233

I updated my question, please have a look at it – neoDev – 2014-03-11T23:16:36.080

4

What is the proper way to close/exit programs from command line,
similar to pressing the "X" close button in the corner of the window?

The answer to that question can be found here(Microsoft link).

You can send WM_CLOSE messages to any window you wish to close. Many windows handle WM_CLOSE to prompt the user to save documents.

A tool that does this correctly is @Kill. Look also SendMsg.


I do not know how to do this in batch, but you could use the vbscript for this. Simulating the Alt + F4 keys (equates to signal WM_CLOSE).

Run and look at the behavior of this script below.

Set WshShell = WScript.CreateObject("WScript.Shell")

' Start Notepad.exe
WshShell.Run "%windir%\notepad.exe"

' Select, or bring Focus to a window named `Notepad`
WshShell.AppActivate "Notepad"

' Wait for 4 seconds
WScript.Sleep 4000

WshShell.SendKeys "Foo"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "Bar"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "{CAPSLOCK}"
WshShell.SendKeys "baz"
WshShell.SendKeys "%{F4}"

Here is the list key names for SendKeys.

When you run the script, the notepad is open, some words are written and then a signal to close the program is delivered, see picture below.


Additional Questions

Can I start a program minimized, or background with vbscript?

Yes. Use the following code:

WshShell.Run "%windir%\notepad.exe", 2

For more information check the Run Method.

Can chrome go to some url in vbscript?

Dim iURL  
Dim objShell iURL = "www.google.com"
set objShell = CreateObject("Shell.Application") 
objShell.ShellExecute "chrome.exe", iURL, "", "", 1

If chrome is the default, use:

set objShell = CreateObject("WScript.Shell")
objShell.run(iURL)

Source

Is there a way to bring focus to specific application (chrome.exe)?

Here in an example.

I want to send alt+f4 ONLY to chrome, independently of i'm doing with other windows.

The following code works on Windows 8.

Dim objShell

iURL = "www.google.com.br"

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run(iURL)

' Select, or bring Focus to Google Chrome
WshShell.AppActivate "Google Chrome"

' Wait for 5 seconds
WScript.Sleep 5000

WshShell.SendKeys "%{F4}"

stderr

Posted 2014-03-11T20:25:46.077

Reputation: 9 300

Can I start a program minimized, or background with vbscript? – neoDev – 2014-03-11T23:43:05.807

1

@Roger If interested, see my answer here on how to combine Batch + JScript into a single file. JScript can do most anything VBScript can do and in a more structured C style.

– David Ruhmann – 2014-03-11T23:54:39.000

and can chrome go to some url in vbscript? – neoDev – 2014-03-11T23:57:02.017

is there a way to bring focus to specific application (chrome.exe)? – neoDev – 2014-03-12T00:34:23.967

Yes I'm trying with it, but if after some delay (WScript.Sleep 5000) i have (WshShell.AppActivate "google chrome"), if during this period i click on some other window, google chrome doesn't come to front, i can see its focus only on the toolbar.... – neoDev – 2014-03-12T01:12:21.013

i want to send alt+f4 ONLY to chrome, independently of i'm doing with other windows.. – neoDev – 2014-03-12T01:14:09.993

I tried it... chrome isnt closed.. when im focus on explorer (folders) only the explorer is closed... (winXP) – neoDev – 2014-03-12T01:27:18.900

@Roger Here worked perfectly (Windows 8). If my answer was helpful to your question, please mark it as accepted. – stderr – 2014-03-12T01:32:43.630

This is a good answer. I tend to keep open 4 or 5 notepad documents, so If only one notepad is open, this will work great. Otherwise, it'll only open 1 notepad and you'll have to activate the script again. That's why i set up the automation to roll through each window below. – Rich – 2014-03-12T01:51:28.500

3

There are some command-line utilities that can send a suitable WM_SYSCOMMAND message (with SC_CLOSE as the command) to a program's top-level window. I'm sure that at least one will be mentioned shortly. (Then someone will mention AutoIt. Then there'll be an answer showing how to do it with PowerShell and CloseMainWindow().)

The command-line utility that comes as a built-in command in JP Software's TCC, a command interpreter and command script processor for Windows, is called TASKEND.

JdeBP

Posted 2014-03-11T20:25:46.077

Reputation: 23 855

I updated my question, please have a look at it – neoDev – 2014-03-11T23:16:04.687

2

Alright, Not going to lie. I saw this on stackoverflow and thought it was a challenging question. Soooo I just spent the last 2 hours writing some code. And here it is....

After following the steps below, you can type "TaskClose notepad.exe" after hitting "Start" and it will auto save all undocumented notepad files into desktop. It will auto-close chrome.exe and save the restoration settings.

You can add and remove additional settings for other applications under the if conditions. For instance:

If InStr(SINGLECLOSEAPPS, WScript.arguments(0)) Then
    SmartSendKeys strOutput,"bypass","%{F4}"

ElseIf InStr(AUTOCLOSEAPPS, WScript.arguments(0)) Then
    SmartSendKeys strOutput,"","%{F4}|%s|{autoname}.txt|%s"

'Example 1: =============================================
ElseIf InStr(WScript.arguments(0), "outlook") Then
    SmartSendKeys strOutput,"","%{F4}" 'Activate Alt+4
'========================================================

'Example 2: =============================================
ElseIf InStr(WScript.arguments(0), "notepad") Then 'I know, already in my autoapps
    SmartSendKeys strOutput,"","%{F4}|%s|{autosave}.txt" 'Activate Alt+4 + Save File + {AutoSave} = autoincrement filename
'========================================================

Else
    SmartSendKeys strOutput,"bypass","%{F4}"
End If

The vbs and batch files performs the following procedures:

  • Collects the executable.
  • Queries the executable application names off of the tasklist.
  • Performs an "Alt+TAB(x)" procedure until it has verified the window is open.
  • Then Executes the rolling commands whether it be "Alt+F4" or even in extreme cases
  • Alt+F4
  • Activate Save
  • AutoIncrememnt Filename
  • Exit application.

ReturnAppList.bat : install in "C:\windows\system32\"

for /f "tokens=10 delims=," %%F in ('tasklist /v /fi "imagename eq %1" /fo csv') do @echo %%~F >>result.txt

TaskClose.bat : install in "C:\windows\system32\" AND "C:\Users\YourUserName\"

C:\windows\system32\wscript.exe c:\windows\system32\taskclose.vbs %1

TaskClose.vbs : install in "C:\windows\system32\"

Set WshShell = CreateObject("WScript.Shell")
Const SINGLECLOSEAPPS = "chrome.exe|iexplore.exe|firefox.exe"
Dim DEFAULTSAVELOCATION : DEFAULTSAVELOCATION = wshshell.SpecialFolders("Desktop")
Const AUTOCLOSEAPPS = "notepad.exe"

Const WshFinished = 1
Const WshFailed = 2
strCommand = "returnapplist.bat "
Set WshShellExec = WshShell.Exec(strCommand & WScript.Arguments(0))

WScript.sleep 2000

Select Case WshShellExec.Status
    Case WshFinished
    strOutput = LoadStringFromFile("result.txt")
    Case WshFailed
    strOutput = LoadStringFromFile("result.txt")
End Select


'SmartSendKeys(application_name_array, bypassclause, additionalcommands)
'========================
If InStr(SINGLECLOSEAPPS, WScript.arguments(0)) Then
    SmartSendKeys strOutput,"bypass","%{F4}"
ElseIf InStr(AUTOCLOSEAPPS, WScript.arguments(0)) Then
    SmartSendKeys strOutput,"","%{F4}|%s|{autoname}.txt|%s"
Else
    SmartSendKeys strOutput,"bypass","%{F4}"
End If




'SmartSendKeys(application_name_array, bypassclause, additionalcommands)
'========================
Function SmartSendkeys(fArr, LoopCount, RollCommands) 
    Dim x 
    Dim splt : splt = Split(farr, vbCrLf)
    If loopcount = "bypass" Then 
        x = 0
    Else
        x = UBound(splt)
    End If
    a = 0
    For s=0 To x
        If Len(splt(s)) > 1 Then
            Set objShell = WScript.CreateObject("WScript.Shell")
            c = 1 : tabs = ""
            Success = False
            Do Until Success = True
                Success = objShell.AppActivate(Trim(splt(s)))
                If success <> True Then
                    If c = 1 Then 
                        tabs = "{TAB}"
                    Else
                        tabs = "{TAB " & c & "}"
                    End If
                    'wscript.echo "Activating: " & "%" & tabs
                    WshShell.SendKeys "%" & tabs
                    WScript.Sleep 5000
                    c = c + 1
                    If c = 100 Then 
                        WScript.echo "App not found"
                        Exit Function
                    End If
                End If
            Loop
            Dim cmds : cmds = Split(rollcommands, "|")

            For Each cm In cmds
                If InStr(cm, "{autoname}") Then
                    Dim file_ext : file_ext = Split(cm, ".") 
                    cm = DEFAULTSAVELOCATION & "autosave" & a & "." & file_ext(1)
                    a = a + 1
                End If
                WshShell.SendKeys cm
                WScript.sleep 500

            Next
        End If
    Next
End Function

Function LoadStringFromFile(filename)
    Const fsoForReading = 1
    Const fsoForWriting = 2
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(filename, fsoForReading)
    Dim fulltext : fulltext = f.ReadAll
    LoadStringFromFile = fulltext
    f.Close
    fso.DeleteFile(filename)
End Function

This was alot of fun to write and I'm more happy about finishing it than actually showing the answer. Have a great week!

Rich

Posted 2014-03-11T20:25:46.077

Reputation: 121