13

I came up with the batch file below, and it is working great. However, I would like to know if there is a way to code it so that if a program is already running, it would skip it and launch the next one. I hope this makes sense. Any advice would be greatly appreciated.

@echo off    
pushd    
start "" cmd /c cscript "C:\Users\User\Desktop\Work.vbs"    
start "C:\Program Files\Microsoft Office\Office15" Outlook.exe    
start "C:\Program Files\Microsoft Office\Office15" Lync.exe    
start "C:\Program Files (x86)\Google\Chrome\Application" chrome.exe    
runas /savecred /user:"DOMAIN\User_Adm" "C:\Program Files (x86)\VMware\Infrastructure\Virtual Infrastructure Client\Launcher\VpxClient.exe"    
runas /savecred /user:"DOMAIN\User_Adm" "mmc.exe \"My_Tools.msc\"
  • psst. powershell is good. – Kolob Canyon Jul 27 '17 at 1:55
20

Here is a example using tasklist to check all running applications for a given name.
Otherwise it starts the program. I'm sure you can adapt it to your needs

tasklist /nh /fi "imagename eq notepad.exe" | find /i "notepad.exe" > nul ||
(start notepad.exe)
  • Make sure this is all on one line, this site formats it so it breaks at || - don't break it – CAD bloke Oct 5 at 5:41
3

I implemented tasklist into my script and its working like a charm.
Here it is for anyone else having the same questions as I had.

@echo off
pushd
tasklist /nh /fi "imagename eq iexplore.exe" | find /i "iexplore.exe" > nul ||(start Work.vbs)
tasklist /nh /fi "imagename eq outlook.exe" | find /i "outlook.exe" > nul ||(start outlook.exe)
tasklist /nh /fi "imagename eq lync.exe" | find /i "lync.exe" > nul ||(start lync.exe)
tasklist /nh /fi "imagename eq chrome.exe" | find /i "chrome.exe" > nul ||(start chrome.exe)
tasklist /nh /fi "imagename eq VpxClient.exe" | find /i "VpxClient.exe" > nul || runas /savecred /user:"DOMAIN\User_Adm" "C:\Program Files (x86)\VMware\Infrastructure\Virtual Infrastructure Client\Launcher\VpxClient.exe"
tasklist /nh /fi "imagename eq mmc.exe" | find /i "mmc.exe" > nul || runas /savecred /user:"DOMAIN\User_Adm" "mmc.exe \"My_Tools.msc\"
3
@echo off      
tasklist /FI "IMAGENAME eq outlook.exe" | find /i "outlook.exe"      

IF ERRORLEVEL 2 GOTO LOOP2
IF ERRORLEVEL 1 GOTO LOOP1 

:LOOP1 
  start notepad.exe
goto EXIT     

:LOOP1 
  start outlook.exe 
goto EXIT 

:EXIT
0

Here is a PowerShell version (instead of CMD).

(You can run powershell from CMD by calling "powershell.exe".

This script does the following:

  1. Checks the process list for a specific process, and if the process is not found in the list...
  2. It will search for the executable in a specific location (like program files), and run it.

In this example, I am starting Skype for Business (AKA "lync").

Here is a 1 liner:

if (!((Get-Process | select ProcessName).ProcessName | where {$_ -like "*lync*"})){&(where.exe /R "C:\Program Files (x86)\Microsoft Office" "lync.exe")}

Here is a commented version:

# If there isn't a running process that contains "lync"...
if (!((Get-Process | select ProcessName).ProcessName | where {$_ -like "*lync*"}))
{
    # Find the executable somewhere in program files (x86), and run it.
    &(where.exe /R "C:\Program Files (x86)\Microsoft Office" "lync.exe")
}

(You don't have to actually search for the executable, you could instead run it directly - however searching for the executable allows for MS Office updates which can sometimes change the install directory)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.