Start programs via command-line, but only if not already running
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
add a comment |
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
psst. powershell is good.
– Kolob Canyon
Jul 27 '17 at 1:55
add a comment |
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
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
windows command-line batch-file
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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)
add a comment |
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"
add a comment |
@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
add a comment |
Here is a PowerShell version (instead of CMD).
(You can run powershell from CMD by calling "powershell.exe
".
This script does the following:
- Checks the process list for a specific process, and if the process is not found in the list...
- 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)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
answered Oct 3 '13 at 21:53
nixdanixda
20.9k878133
20.9k878133
add a comment |
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
edited Oct 5 '13 at 15:00
nixda
20.9k878133
20.9k878133
answered Oct 4 '13 at 20:03
user259671user259671
93116
93116
add a comment |
add a comment |
@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
add a comment |
@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
add a comment |
@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
@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
edited Aug 19 '15 at 5:54
nixda
20.9k878133
20.9k878133
answered Jul 15 '14 at 11:20
SantoshSantosh
311
311
add a comment |
add a comment |
Here is a PowerShell version (instead of CMD).
(You can run powershell from CMD by calling "powershell.exe
".
This script does the following:
- Checks the process list for a specific process, and if the process is not found in the list...
- 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)
add a comment |
Here is a PowerShell version (instead of CMD).
(You can run powershell from CMD by calling "powershell.exe
".
This script does the following:
- Checks the process list for a specific process, and if the process is not found in the list...
- 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)
add a comment |
Here is a PowerShell version (instead of CMD).
(You can run powershell from CMD by calling "powershell.exe
".
This script does the following:
- Checks the process list for a specific process, and if the process is not found in the list...
- 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)
Here is a PowerShell version (instead of CMD).
(You can run powershell from CMD by calling "powershell.exe
".
This script does the following:
- Checks the process list for a specific process, and if the process is not found in the list...
- 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)
edited Jan 11 at 15:57
answered Jan 11 at 15:26
NullldataNullldata
1014
1014
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
psst. powershell is good.
– Kolob Canyon
Jul 27 '17 at 1:55