Joe Lara
Joe Lara

Reputation: 33

vbs taskkill by name

I'am trying to find how to close a process using it's title.

I found the command:

taskkill /fi "WINDOWTITLE eq the_title_of_the_windows"

and it works great.

When I try:

oShell.Run "taskkill /fi "WINDOWTITLE eq the_title_of_the_windows"", , True

I get an error and it won't compile.

Any idea on how to use th symbole " in this line?

Upvotes: 3

Views: 18818

Answers (3)

Prabhat Kumar
Prabhat Kumar

Reputation: 11

Alternatively you can try below code: This code will pick the task from Task manager and close the process. Copy pasted the code in ".vbs" File and use call KillAll("your task name.exe")

Function KillAll(ProcessName)
    Dim objWMIService, colProcess
    Dim strComputer, strList, p
    Dim i :i= 0 
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name like '" & ProcessName & "'")
    For Each p in colProcess
        p.Terminate     
    i = i+1        
    Next
    MsgBox("Total Instance :: " &i& " of "&ProcessName&" is killed")
End Function

call KillAll("MicrosoftEdge.exe")

Upvotes: 1

hollopost
hollopost

Reputation: 597

If you use Run to execute the command line you will find ugly dos windows popup in the screen to avoid that use one of two ways:

Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.Exec "taskkill /fi ""WINDOWTITLE eq Calculator"""

OR

Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.Run "taskkill /fi ""WINDOWTITLE eq Calculator""",0,False

Upvotes: 0

41686d6564
41686d6564

Reputation: 19651

In order to use double quotation marks inside another pair of double quotation marks, you need to use "" instead of just ", because if you use one quotation mark " it will be considered the end of text between the first and the second quotation marks

So, your code should look like this:

oShell.Run "taskkill /fi ""WINDOWTITLE eq the_title_of_the_windows""", , True

The following example will terminate all processes with window title (Calculator):

Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.Run "taskkill /fi ""WINDOWTITLE eq Calculator""", , True

Hope that helps :)

Upvotes: 5

Related Questions