Search code examples
vbscriptprocess

Get a process path with VBS


I want to kill a process with vbs, i know the command is :

oShell.Run "taskkill /im software.exe", , True

how to get software.exe path before we kill it ?

Because i want to launch software.exe again.


Solution

  • WMI (can teminate as well):

    dim wmi, list, process, path, shell
    
    set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
    set list = wmi.ExecQuery("Select * from Win32_Process") 
                      '// or "Select * from Win32_Process where name = 'xxxxxxx.exe'" allowing the removal of the if block
    
    for each process in list
        if (lcase(process.name) = "xxxxxxx.exe") then
            path = process.ExecutablePath
            process.terminate()
            exit for
        end if
    next
    
    wscript.echo path
    
    set shell = CreateObject("WScript.Shell")
    shell.Run Chr(34) & path & Chr(34)