New Launch Method
This method launches any program and gets the PID, and ideally should be the standard moving forward:
powershell -executionPolicy bypass -command "& {$process = start-process $args[0] -passthru -argumentlist $args[1..($args.length-1)]; exit $process.id}" FileToRun.bat Parameters
echo Process ID of new process: %errorlevel%
Self-Identify PID in Batch file
While this does not let you identify the PID of other Batch Files, this method allows a batch file to self identify its PID:
FOR /F "tokens=*" %%G in ('powershell -ExecutionPolicy Unrestricted -Command "(New-Guid).guid"') do Set my_guid=%%~G
powershell -ExecutionPolicy Unrestricted -Command "(Get-WmiObject Win32_Process -Filter ProcessId=$PID).ParentProcessId>"%temp%\%my_guid%.pid"
FOR /F "tokens=*" %%P in ('type "%temp%\%my_guid%.pid"') do set my_pid=%%~P
del /f /q "%temp%\%my_guid%.pid"
(Note that a temp file must be used because when the command is run in a for loop, the for loop becomes the parent process).
Launch Batch File in Traditional Conhost.exe
You can also launch cmd.exe windows in the traditional window, which will be backward compatible with other methods.
cmd.exe was not the original emulator that Windows Terminal replaces. cmd.exe is a command processor that runs in the emulator. The traditional emulator was conhost.exe.
As such you can run a batch file or cmd.exe directly in conhost:
conhost.exe Listener.bat
You can launch a batch file in the background with a VBScript as follows:
Set oShell = CreateObject("WScript.Shell")
oShell.Run """Listener.bat""", 0
This still runs it in the traditional conhost.exe instance and works with tasklist.
Bug Report to Microsoft
I did add also add a Problem Report to the feedback hub "Windows Terminal Blocks tasklist from seeing cmd.exe window titles [Breaks Batch Files]"