Windows – How to check if a process is running via a batch script

batch-filecommand linewindows

How can I check if an application is running from a batch (well cmd) file?

I need to not launch another instance if a program is already running. (I can't change the app to make it single instance only.)

Also the application could be running as any user.

Best Answer

Another possibility I came up with, which does not require to save a file, inspired by using grep is:

tasklist /fi "ImageName eq MyApp.exe" /fo csv 2>NUL | find /I "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Program is running
  • /fi "" defines a filter of apps to find, in our case it's the *.exe name
  • /fo csv defines the output format, csv is required because by default the name of the executable may be truncated if it is too long and thus wouldn't be matched by find later.
  • find /I means case-insensitive matching and may be omitted

See the man page of the tasklist command for the whole syntax.