Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Task : Launch X after application after Y launched

06 Aug 2013   #11
Kari

Microsoft Community Contributor Award Recipient

 

I will play with this a bit later, but here's my idea if someone wants to give it a try already:
  • Create a PowerShell script
  • Start first program:
    • Code:
      [System.Diagnostics.Process]::Start("Program_1").WaitForExit(5000)
    • Wait for Exit delay is set for 5 seconds (5,000 milliseconds); if the Program_1 is not closed within this time frame, executing the script continues. This will give the Program_1 some time to completely start before Program_2 is launched, increase the value if 5 seconds is not enough
  • Start Program_2, this time with Wait for Exit switch with no value:
    • Code:
      [System.Diagnostics.Process]::Start("Program_2").WaitForExit()
    • The Wait for Exit switch without a value causes the script to halt and wait until the Program_2 is closed
  • Now kill first task (program) with Taskkill
Should work.

Kari


My System SpecsSystem Spec
.
06 Aug 2013   #12
Kari

Microsoft Community Contributor Award Recipient

 

OK, here's the PowerShell script, delay between launch of Program_1 and Program_2 set to 10 seconds (10,000 milliseconds). Use Notepad or other text editor to write the script, save it with .ps1 extension, for instance F:\Scripts\MyScript.ps1.

The instructions might look complicated but this really is quite fast and easy.

This script would work as follows:
  • Start Program 1
  • Wait given time, in this example 10 seconds
  • Start Program 2
  • When Program 2 is manually closed, close Program 1
  • Exit PowerShell
1. The script:
Code:
[System.Diagnostics.Process]::Start("Path_to_Program_1").WaitForExit(10000)
[System.Diagnostics.Process]::Start("Path_to_Program_2").WaitForExit()
taskkill /IM Name_of_the_process_Program_1
exit
Highlighted parts:
  • Lines 1 and 2:
    Name and path of Programs 1 & 2, for instance C:\Windows\System32\notepad.exe or D:\Games\MyGame\Rally.exe
  • Line 3:
    Process (image) name for process (program) 1, for example notepad.exe. The second process needs no Taskkill as Program_2 is already manually closed for the script to continue.
2. Change PowerShell Script Execution Policy:

Before running the script you have to change the PowerShell Execution Policy. This must be done only once, any change in policy will be active until the next time you decide to change it. By default the policy is set to Restricted, meaning no scripts are allowed to run.

To do this run PowerShell elevated (as administrator) and change the policy as told here:
  • Click Start
  • Type PowerShell to Start Menu Search field
  • Right click PowerShell
  • Select Run as administrator
    Task : Launch X after application after Y launched-2013-08-07_140151.png
  • Accept the UAC prompt by clicking Yes
  • Type the following command to PowerShell, press Enter:
    Set-ExecutionPolicy Unrestricted
  • Accept the policy change by typing Y, press Enter
    Task : Launch X after application after Y launched-2013-08-07_140406.png
  • The policy is now changed, you can close the elevated PowerShell window and continue from below.

    3. Run the script:
    • Save the script with .ps1 extension, for instance F:\Scripts\MyScript.ps1
    • Now simply run the script using Run dialog (WIN + R) giving the following command or create a shortcut for this command:
      powershell.exe F:\Scripts\MyScript.ps1
    Notice please: If the launch of the Program_2 does not need to wait until the Program_1 has completely started, leave the Wait for Exit switch away from first line. In this case the script would be:
    Code:
    [System.Diagnostics.Process]::Start("Path_to_Program_1")
    [System.Diagnostics.Process]::Start("Path_to_Program_2").WaitForExit()
    taskkill /IM Name_of_the_process_Program_1
    exit
    That's it.

    Kari


    My System SpecsSystem Spec
    07 Aug 2013   #13
    unko

    Win7x64
     
     

    Thanks I'm going to try it but I see a problem, you wrote when program 2 is closed, program 1 will close, right? It's the opposite, since program 1 is the game, that's this one I'll close manually...
    My System SpecsSystem Spec
    .

    07 Aug 2013   #14
    Kari

    Microsoft Community Contributor Award Recipient

     

    Quote   Quote: Originally Posted by unko View Post
    Thanks I'm going to try it but I see a problem, you wrote when program 2 is closed, program 1 will close, right? It's the opposite, since program 1 is the game, that's this one I'll close manually...
    I really cannot see what's the issue? The same script, now using CPU Throttle and Game as example programs:
    Code:
    [System.Diagnostics.Process]::Start("Path_to_CPU Throttle")
    [System.Diagnostics.Process]::Start("Path_to_Game").WaitForExit()
    taskkill /IM Name_of_the_process_CPU Throttle
    exit
    That's it. CPU Throttle and the Game are launched. When the Game is manually closed, system stops the CPU Throttle. Exactly what you wanted:
    Quote   Quote: Originally Posted by unko View Post
    Anytime i launch a game, i want CPU Throttle to launch. And when i close that game, i want it to close too!
    About one of your requirements:
    Quote   Quote: Originally Posted by unko View Post
    Really need a script that's always running and monitoring, automatically launched at boot.
    This is absolutely unnecessary. The script I have given to you launches both CPU Throttle and the Game, then closes CPU Throttle when the Game is manually closed.

    Kari

    Kari
    My System SpecsSystem Spec
    07 Aug 2013   #15
    unko

    Win7x64
     
     

    I just tried to launch it but got error, checked and it says "use of script is disabled on this system". Also .PS1 extension is by another app (PSXMemTool) but i guess that's not important. There's something i'm not sure of still though : When you launch this script, it'll automatically launch both of the apps and close the one of your choice in the end, right?

    If so that means i would have to create a script for each games? That's not what i asked first. I wanted a script to run all the time, monitoring all process, so as soon as it detect i launched one of the exe from a list (games), it'll launch CPU Throttle, and when closing a game from that list, close CPU Throttle. But maybe that's not possible..? I have no knowledge for those things. You tell me. Thanks!
    My System SpecsSystem Spec
    07 Aug 2013   #16
    Kari

    Microsoft Community Contributor Award Recipient

     

    Quote   Quote: Originally Posted by unko View Post
    I just tried to launch it but got error, checked and it says "use of script is disabled on this system". Also .PS1 extension is by another app (PSXMemTool) but i guess that's not important. There's something i'm not sure of still though : When you launch this script, it'll automatically launch both of the apps and close the one of your choice in the end, right?
    Right.

    I forgot to mention that scripts must first be enabled. As I always enable the PowerShell scripts I simply forgot the whole thing.

    I am not at home now, please give me an hour or two and I will edit my original post accordingly to show how to enable scripting. In the mean time, here's something for you to read:
    Quote   Quote: Originally Posted by unko View Post
    If so that means i would have to create a script for each games? That's not what i asked first. I wanted a script to run all the time, monitoring all process, so as soon as it detect i launched one of the exe from a list (games), it'll launch CPU Throttle, and when closing a game from that list, close CPU Throttle. But maybe that's not possible..? I have no knowledge for those things. You tell me. Thanks!
    With native Windows batch and scripting tools, to achieve what you want to is impossible, as far as I know. In my opinion the better alternative is to prepare shortcuts for each game, each shortcut launching a script that starts CPU Throttle and the respective game, then closes CPU Throttle when the game is closed.

    Kari
    My System SpecsSystem Spec
    07 Aug 2013   #17
    Kari

    Microsoft Community Contributor Award Recipient

     

    OK, the original post is now more like a mini tutorial, it is edited to cover everything you need to know to enable PowerShell scripts and create a script that launches multiple programs and closes them all when the last program launched by the script is manually closed.

    Kari
    My System SpecsSystem Spec
    07 Aug 2013   #18
    unko

    Win7x64
     
     

    Quote   Quote: Originally Posted by Kari View Post
    Quote   Quote: Originally Posted by unko View Post
    If so that means i would have to create a script for each games? That's not what i asked first. I wanted a script to run all the time, monitoring all process, so as soon as it detect i launched one of the exe from a list (games), it'll launch CPU Throttle, and when closing a game from that list, close CPU Throttle. But maybe that's not possible..? I have no knowledge for those things. You tell me. Thanks!
    With native Windows batch and scripting tools, to achieve what you want to is impossible, as far as I know. In my opinion the better alternative is to prepare shortcuts for each game, each shortcut launching a script that starts CPU Throttle and the respective game, then closes CPU Throttle when the game is closed.

    Kari
    Hmm ok well thanks for all, i'll have a look into it, and if someone knows a way to do the full thing i wanted, let us know. I guess it would be an app doing that then, not a script.
    My System SpecsSystem Spec
    07 Aug 2013   #19
    MilesAhead

    Windows 7 32 bit
     
     

    Take a look at RunGroup
    My System SpecsSystem Spec
    07 Aug 2013   #20
    unko

    Win7x64
     
     

    Link's dead ands I cannot find it anywhere else Looked good..
    My System SpecsSystem Spec
    Reply

     Task : Launch X after application after Y launched




    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search




    Similar help and support threads
    Thread Forum
    Opening a GUI application launched through Telnet
    Dear all, I am having trouble with Windows 7, as I have migrated my computers from Windows 2000, and I am not able to run one application. The problem I have is that I used to launch a process from a Unix machine through telnet into my machine. For an unknown reason, I am able to start the...
    Network & Sharing
    BSOD while no application is launched
    Hi, from a year now, I got BOSD every two days. Sometimes I work into 3DS max, sometimes I am not in front of my PC, sometimes I use photoshop or sometimes I am just on Internet (and I had one during writing this post which is not in the report...) Could someone help me ? My configuration...
    BSOD Help and Support
    Can't launch link in application
    My friend uses Sopcast to watch sporting events. The way it should work is you click a link in your browser that looks like this: sop://broker.sopcast.com:3912/12345 and the right channel should open up in the Sopcast software. They use Windows 7 and IE 9, but it only works right for them from...
    Browsers & Mail
    Can Launchy launch the task manager?
    The Launchy forums seem all but dead, so I figured I would ask here. Does anybody know if you can pull up your task manager using Launchy? thank you :geek: Launchy: The Open Source Keystroke Launcher
    Software
    Problem When I launch an Application.
    When I launch an application, blender, I get a big error message, assessing that the configuration isn't right. Attached is a snapshot of the problem. Any help/ideas?! Many thanks! = )
    Software
    Quick launch / task bar
    I have several quick launch icons on my task bar however it seems that when programs open or I launch IE it removes the icons and puts the open window tile in the task bar. Only if I right click on some of them do I see the missing icons. I have the bar locked but it still shuffles stuff around. Am...
    Customization


    Our Sites

    Site Links

    About Us

    Find Us

    Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd

    All times are GMT -5. The time now is 17:21.

    Twitter Facebook Google+



    Windows 7 Forums

    Seven Forums Android App Seven Forums IOS App