@echo off

setlocal

@REM Handle command line args
set "ImageName=%1"
set "JustMySession=%2"
set "PauseAfter=%3"

@REM Set session ID constraint (if restricting by session)
set "MySessionId=-1"
for /f "tokens=3-4" %%a in ('query session %USERNAME%') do @if "%%b"=="Active" set MySessionId=%%a

@REM @rem Show tasklist, filtering by image name and (optionally) session ID
if /i "%JustMySession%" == "true" (
    tasklist /fi "imagename eq %ImageName%" /fi "session eq %MySessionId%"
) else (
    tasklist /fi "imagename eq %ImageName%"
)

@REM @rem Pause (if desired)
if /i "%PauseAfter%" == "true" pause

endlocal
