<p>Imagine a DOS style .cmd file which is used to launch interdependent windowed applications in the right order.</p>

<p>Example:<br>
1) Launch a server application by calling an exe with parameters.<br>
2) Wait for the server to become initialized (or a fixed amount of time).<br>
3) Launch client application by calling an exe with parameters.</p>

<p>What is the simplest way of accomplishing this kind of batch job in PowerShell?</p>

[{'answer_id': 49520, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 0, 'selected': False, 'text': "<p>To wait 10 seconds between launching the applications, try</p>\n\n<pre><code>launch-server-application serverparam1 serverparam2 ...\nStart-Sleep -s 10\nlaunch-client-application clientparam1 clientparam2 clientparam3 ...\n</code></pre>\n\n<p>If you want to create a script and have the arguments passed in, create a file called runlinkedapps.ps1 (or whatever) with these contents:</p>\n\n<pre><code>launch-server-application $args[0] $args[1]\nStart-Sleep -s 10\nlaunch-client-application $args[2] $args[3] $args[4]\n</code></pre>\n\n<p>Or however you choose to distribute the server and client parameters on the line you use to run runlinkedapps.ps1. If you want, you could even pass in the delay here, instead of hardcoding <code>10</code>.</p>\n\n<p>Remember, your .ps1 file need to be on your Path, or you'll have to specify its location when you run it. (Oh, and I've assumed that launch-server-application and launch-client-application are on your Path - if not, you'll need to specify the full path to them as well.)</p>\n"}, {'answer_id': 49540, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 4, 'selected': True, 'text': '<p>Remember that PowerShell can access .Net objects. The Start-Sleep as suggested by <a href="https://stackoverflow.com/questions/49402/creating-batch-jobs-in-powershell#49520">Blair Conrad</a> can be replaced by a call to <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforinputidle.aspx" rel="nofollow noreferrer">WaitForInputIdle</a> of the server process so you know when the server is ready before starting the client.</p>\n\n<pre><code>$sp = get-process server-application\n$sp.WaitForInputIdle()\n</code></pre>\n\n<p>You could also use <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx" rel="nofollow noreferrer">Process.Start</a> to start the process and have it return the exact Process. Then you don\'t need the get-process.</p>\n\n<pre><code>$sp = [diagnostics.process]::start("server-application", "params")\n$sp.WaitForInputIdle()\n$cp = [diagnostics.process]::start("client-application", "params")\n</code></pre>\n'}, {'answer_id': 49598, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 1, 'selected': False, 'text': '<p>@Lars Truijens suggested</p>\n\n<blockquote>\n  <p>Remember that PowerShell can access\n  .Net objects. The Start-Sleep as\n  suggested by Blair Conrad can be\n  replaced by a call to WaitForInputIdle\n  of the server process so you know when\n  the server is ready before starting\n  the client.</p>\n</blockquote>\n\n<p>This is more elegant than sleeping for a fixed (or supplied via parameter) amount of time. However, \n<a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforinputidle.aspx" rel="nofollow noreferrer">WaitForInputIdle</a> </p>\n\n<blockquote>\n  <p>applies only to processes with a user\n  interface and, therefore, a message\n  loop.</p>\n</blockquote>\n\n<p>so this may not work, depending on the characteristics of launch-server-application. However, as Lars pointed out to me, the question referred to a <em>windowed</em> application (which I missed when I read the question), so his solution is probably best.</p>\n'}]
['https://Stackoverflow.com/questions/49402', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5085/']