Windows analog of $1 (PID of the last started process)

Posted on


How to get on Windows PID of the last started process using WScript.Shell or similar?

I know how to start a process and get its PID:

Set oExec = WshShell.Exec( program )
Set pid = oExec.ProcessID

Now the problem is that the program is another batch (or could be VBScript). The batch starts a real program which PID I would like to get.

On Linux (shell script) it is very easy to do. just pid=$!

But what can I do on Windows?


I think there is no equivalent of $! In Windows.

In case you wish to retrieve the PID of the third process, you can search for it using WMI. A example of this could be found here (just searching and displaying the information) or here (searching and killing the process).

If the executable of the third name is not known, you might want to use a Job object which groups several processes into one object. And you could easily kill all of them at once with TerminateJobObject.

See MSDN for a quick overview or this question on StackOverflow.

However, I don’t know if these methods can be used from VBScript.

Leave a Reply

Your email address will not be published.