For those looking for a solution other than Powershell, here is a script package that uses cscript to invoke a JScript block. JScript creates a new child process, gets its PID, and then approaches the line ParentProcessID ancestors until it reaches explorer.exe , and then returns the PID of the direct child. It should return the correct PID for the console window in which the script is running, even if there are multiple instances of cmd.exe or cscript.exe .
What can I say? Today I felt creative.
@if (@ a==@b ) @end var oShell = WSH.CreateObject('wscript.shell'), johnConnor = oShell.Exec('%comspec% /k @echo;');
source share