Search code examples
vbscriptdostitlehtataskkill

How to kill specific HTA based on window title


I have an HTA file with the filepath: C:\Users\ME\Desktop\DataTable.hta which has a window title of DataTable as declared in its code using <title>DataTable</title>

I'm trying to close this specific HTA window using DOS, javascript or vbscript. however, when I try to use taskkill in the following fashion its doesn't close. It works fine for notepad and other windows, but not for HTAs.

I type this into dos:

taskkill /FI "WINDOWTITLE eq DataTable

and nothing happens. Yet if I use:

taskkill /FI "WINDOWTITLE eq Untitled - Notepad

it successfully closes notepad. Why won't it work for HTAs? Is there a solution?

Thank you.


Solution

  • We presume that you are running a HTA with this name : DataTable.hta.

    So,we can kill this HTA by its name using a vbscript like that :

    Option Explicit
    Call KillProcessbyName("DataTable.hta")
    '**********************************************************************************************
    Sub KillProcessbyName(FileName)
        On Error Resume Next
        Dim WshShell,strComputer,objWMIService,colProcesses,objProcess
        Set WshShell = CreateObject("Wscript.Shell")
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
        For Each objProcess in colProcesses
            If InStr(UCase(objProcess.CommandLine),UCase(FileName)) > 0 Then
                If Err <> 0 Then
                    MsgBox Err.Description,VbCritical,Err.Description
                Else
                    objProcess.Terminate(0) 
                End if
            End If
        Next
    End Sub
    '**********************************************************************************************