United Kingdom
Asked — Edited

Closing A Program Using Exec()

Quick question. As the title suggests, after opening a program or file in Windows 8 using the EXEC() command (e.g, opening a picture), how can you close that program or file using the execute command?



Upgrade to ARC Pro

Unlock the true power of automation and robotics by becoming a proud subscriber of Synthiam ARC Pro.


From a script I would do something like this...

$attributes = "/IM process.exe>nul"
Exec( "taskkill", $attributes ) 

process.exe would be the name of the exe that you want to kill.

This is a post on another forum that I found about a similar topic.

"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.

WM_CLOSE vs SC_CLOSE How to Gracefully Terminate a Process How To Terminate an Application "Cleanly" in Win32

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 Article that I have linked."

United Kingdom

Thanks David. I'll give it a try.

United Kingdom


I gave it a shot. When I open the CMD prompt window and type"TaskList" the list shows shows what's running and had success using "TaskKill" closing my web browser, but I can't seem to find the correct image name or PID code for things like Windows photo viewer or video player which are open. They are more than likely there but don't recognise the correct image name.

Haven't tried your script yet.


let me see if I can find the name for the photo viewer or video player. I am sure they are named something weird. Give me a moment and I will try to find what they are called.


taskkill /F /IM wmplayer.exe for Media Player. Trying to find the Windows Photo Viewer.


taskkill /F /IM PhotosApp.exe will work for the Photos in Windows 8.

United Kingdom

Not sure what I'm doing wrong, taskkill /F /IM PhotosApp.exe didn't close the app down for me.

exec("C:\Users\steve_000\Pictures\EZ robot project showcase\k9 1.png")


$attributes = "IM PhotosApp.exe>nul"

exec("C:\Users\steve_000\Pictures\EZ robot project showcase\k9 1.png")


$attributes = "/F /IM PhotosApp.exe>nul"
$attributes = "/F /IM dllhost.exe>nul"

United Kingdom

Lol, still not doing what it should.

What is the "PhotosApp" for? Is it for the "Windows Photo viewer" app or the "Photos" app? Either way, even with the script you just supplied David, it opens the photo, but doesn't close it (although I do see the CMD window flash up very quickly after the 2 second sleep).


hmm, interesting. The DLLHost exe is what was keeping it open for me. Try this...

Open the photo.

From a command line try this taskkill /F /IM PhotosApp.exe

Tell me what the next line says.

Then, try this taskkill /F /IM dllhost.exe

Tell me what the next line says.

I am looking to see if a line like this shows up "SUCCESS: The process "PhotosApp.exe" with PID 8312 has been terminated." and then "SUCCESS: The process "dllhost.exe" with PID 7568 has been terminated."

United Kingdom

Having the photos open with Windows PhotonViewer as default, opening a photo and using the PID code for "dllhost.exe" closes the photo showing...

"SUCCSESS: Sent termination signal to the process with PID 6464.

so all good there.

But within ARC, it's a different matter. Running the script opens the photo, sleeps for 2 seconds, then I see the CMD window flash once, but photo remains open with the script console saying "Done".

exec("C:\Users\steve_000\Pictures\EZ robot project showcase\k9 1.png")


$attributes = "/F /IM dllhost.exe>nul"

This works for me from ARC script.

exec("C:\current build progress.jpg")
$attributes = "/F /IM dllhost.exe"
Exec( "taskkill", $attributes )

United Kingdom

Thanks buddy. I'll keep trying things out my end.


it may be the >nul that was causing the issue. Try to remove that.

United Kingdom

Yep, you nailed it my friend. Your last script worked for me too.

That's 2 for 2 David. Once again, thanks very much for your help. Most grateful.:)


Glad we found a solution and happy to help. Thanks Steve.