VBS to close all open applications

Howdy team,

I'm new to VBS and need some help here. I have written a script that works very well, but need to close all open applications before it executes. These will be on remote laptops, and no one will be using them when the script executes.

How can I force to close all open applications before the rest of my script runs?

April 24th, 2014 5:24pm

Why do you need to close all applications before the script runs?
Free Windows Admin Tool Kit Click here and download it now
April 24th, 2014 5:43pm

The script logs into a website then grabs some information from it. If the user is already logged in, the script will not run successfully. I need to ensure that the script logs in to the site. The best way I can imagine to do this is to have all browsers closed.
April 24th, 2014 5:47pm

So your question isn't about closing all open applications, but closing running instances of a browser (IE?)?

Free Windows Admin Tool Kit Click here and download it now
April 24th, 2014 5:51pm

Yes. IE and Chrome.

However, if they are not open I need the script to run without user input. 

April 24th, 2014 5:54pm

What about other browsers?

You can terminate a running process using a script using WMI. The repository should have examples.

Free Windows Admin Tool Kit Click here and download it now
April 24th, 2014 5:56pm

All,

I have searched the repository for a method of closing an open application (Chrome). Code is below. I am stuck on two issues:

1. I am getting a Not Found error on Line 9, Char 6. Code: 80041002, Source: SWbemObjectEx. I'm new to VBS and am not sure what this means. The script DOES close Chrome, but also gives me this error.

2. Upon restarting Chrome, there is the notice that Chrome did not shut down correctly. Is there anyway to eliminate this?

I am trying to complete automate some tasks, and need zero user interaction.

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colProcessList = objWMIService.ExecQuery _ 
    ("Select * from Win32_Process Where Name = 'Chrome.exe'") 
 
For Each objProcess in colProcessList 
    objProcess.Terminate() 
	Next

I appreciate your help.

April 29th, 2014 10:49pm

Problem #1 can probably be avoided by careful use of the On Error Resume Next statement. Place it just above your For Each statement.

If the program you're closing doesn't provide a programmatic way of shutting it down gracefully and you forcibly terminate the process, then you cannot avoid problem #2.

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2014 11:55pm

The only way I could get this to shutdown gracefully was to use taskkill.exe inside the VBS file. This would allow me to open Chrome (after it had been closed programmatically) without the error message that it had be closed improperly.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Chrome.exe'")
 
Set oShell = CreateObject("WScript.Shell")
For Each objProcess in colProcessList
    oShell.Run "taskkill /im chrome.exe", , True
Next

Honestly though, if you're new to VBS, then you might consider being new to PowerShell instead. Here's what this looks like in PowerShell:

Get-Process -Name 'chrome' | Stop-Process -Force

Note: This one seems to be hit or miss on whether Chrome reports it was closed improperly or not, so.... Here's PowerShell using taskkill. Notice that this example uses taskkill's /pid switch and not the /im switch from the VBS example above. Have fun!

$Chrome = Get-Process -Name chrome | select -ExpandProperty ID
Foreach ($C in $Chrome) {
    taskkill /pid $C
}
Edit: Added PowerShell examples



  • Edited by tommymaynard Tuesday, April 29, 2014 9:16 PM
  • Marked as answer by TWRX79 16 hours 8 minutes ago
April 30th, 2014 12:07am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics