Start programs via command-line, but only if not already running












12















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:UsersUserDesktopWork.vbs"
start "C:Program FilesMicrosoft OfficeOffice15" Outlook.exe
start "C:Program FilesMicrosoft OfficeOffice15" Lync.exe
start "C:Program Files (x86)GoogleChromeApplication" chrome.exe
runas /savecred /user:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"









share|improve this question

























  • psst. powershell is good.

    – Kolob Canyon
    Jul 27 '17 at 1:55
















12















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:UsersUserDesktopWork.vbs"
start "C:Program FilesMicrosoft OfficeOffice15" Outlook.exe
start "C:Program FilesMicrosoft OfficeOffice15" Lync.exe
start "C:Program Files (x86)GoogleChromeApplication" chrome.exe
runas /savecred /user:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"









share|improve this question

























  • psst. powershell is good.

    – Kolob Canyon
    Jul 27 '17 at 1:55














12












12








12


1






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:UsersUserDesktopWork.vbs"
start "C:Program FilesMicrosoft OfficeOffice15" Outlook.exe
start "C:Program FilesMicrosoft OfficeOffice15" Lync.exe
start "C:Program Files (x86)GoogleChromeApplication" chrome.exe
runas /savecred /user:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"









share|improve this question
















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:UsersUserDesktopWork.vbs"
start "C:Program FilesMicrosoft OfficeOffice15" Outlook.exe
start "C:Program FilesMicrosoft OfficeOffice15" Lync.exe
start "C:Program Files (x86)GoogleChromeApplication" chrome.exe
runas /savecred /user:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"






windows command-line batch-file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 6 '13 at 14:11









Hennes

59.1k792141




59.1k792141










asked Oct 3 '13 at 21:22









user259671user259671

93116




93116













  • psst. powershell is good.

    – Kolob Canyon
    Jul 27 '17 at 1:55



















  • psst. powershell is good.

    – Kolob Canyon
    Jul 27 '17 at 1:55

















psst. powershell is good.

– Kolob Canyon
Jul 27 '17 at 1:55





psst. powershell is good.

– Kolob Canyon
Jul 27 '17 at 1:55










4 Answers
4






active

oldest

votes


















19














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)





share|improve this answer































    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:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
    tasklist /nh /fi "imagename eq mmc.exe" | find /i "mmc.exe" > nul || runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"





    share|improve this answer

































      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





      share|improve this answer

































        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)






        share|improve this answer

























          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f654088%2fstart-programs-via-command-line-but-only-if-not-already-running%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          19














          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)





          share|improve this answer




























            19














            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)





            share|improve this answer


























              19












              19








              19







              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)





              share|improve this answer













              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)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 3 '13 at 21:53









              nixdanixda

              21k1078133




              21k1078133

























                  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:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
                  tasklist /nh /fi "imagename eq mmc.exe" | find /i "mmc.exe" > nul || runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"





                  share|improve this answer






























                    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:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
                    tasklist /nh /fi "imagename eq mmc.exe" | find /i "mmc.exe" > nul || runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"





                    share|improve this answer




























                      3












                      3








                      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:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
                      tasklist /nh /fi "imagename eq mmc.exe" | find /i "mmc.exe" > nul || runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"





                      share|improve this answer















                      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:"DOMAINUser_Adm" "C:Program Files (x86)VMwareInfrastructureVirtual Infrastructure ClientLauncherVpxClient.exe"
                      tasklist /nh /fi "imagename eq mmc.exe" | find /i "mmc.exe" > nul || runas /savecred /user:"DOMAINUser_Adm" "mmc.exe "My_Tools.msc"






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Oct 5 '13 at 15:00









                      nixda

                      21k1078133




                      21k1078133










                      answered Oct 4 '13 at 20:03









                      user259671user259671

                      93116




                      93116























                          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





                          share|improve this answer






























                            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





                            share|improve this answer




























                              3












                              3








                              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





                              share|improve this answer















                              @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






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Aug 19 '15 at 5:54









                              nixda

                              21k1078133




                              21k1078133










                              answered Jul 15 '14 at 11:20









                              SantoshSantosh

                              311




                              311























                                  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)






                                  share|improve this answer






























                                    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)






                                    share|improve this answer




























                                      0












                                      0








                                      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)






                                      share|improve this answer















                                      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)







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jan 11 at 15:57

























                                      answered Jan 11 at 15:26









                                      NullldataNullldata

                                      1014




                                      1014






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Super User!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f654088%2fstart-programs-via-command-line-but-only-if-not-already-running%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                          Alcedinidae

                                          Origin of the phrase “under your belt”?