{"id":63473,"date":"2007-12-04T01:00:00","date_gmt":"2007-12-04T01:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/12\/04\/hey-scripting-guy-how-can-i-start-an-application-from-a-windows-powershell-script-then-pause-the-script-until-the-application-has-terminated\/"},"modified":"2007-12-04T01:00:00","modified_gmt":"2007-12-04T01:00:00","slug":"hey-scripting-guy-how-can-i-start-an-application-from-a-windows-powershell-script-then-pause-the-script-until-the-application-has-terminated","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-start-an-application-from-a-windows-powershell-script-then-pause-the-script-until-the-application-has-terminated\/","title":{"rendered":"Hey, Scripting Guy! How Can I Start an Application From a Windows PowerShell Script, Then Pause the Script Until the Application Has Terminated?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! In Windows PowerShell, how can I start another application, then have my script pause until that application has terminated?<BR><BR>&#8212; AD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, AD. Before we answer today\u2019s question, we\u2019d like to make an announcement: the Scripting Guys need friends!<\/P>\n<P>No, not those kind of friends. Well, come to think of it, we <I>could<\/I> use some regular friends, too. For now, however, we\u2019re specifically looking for some new Facebook friends. Just this morning we created a <A href=\"http:\/\/www.facebook.com\/\" target=\"_blank\"><B>Facebook page<\/B><\/A> for the Scripting Guys, but \u2013 alas \u2013 we don\u2019t have many friends; in fact, the only members of the group are Greg Stemp (who happens to be a dead ringer for Dr. Scripto), and Jean Ross (who bears an uncanny resemblance to a question mark):<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/facebook1.jpg\" width=\"411\" height=\"325\"> \n<P><BR>Now, we aren\u2019t saying that Jean and Greg aren\u2019t a lot of fun, but \u2013 well, OK, so maybe we <I>are<\/I> saying that Jean and Greg aren\u2019t a lot of fun. (Greg\u2019s actually OK, but Jean looks like a question mark, for Pete\u2019s sake!) That\u2019s why we\u2019re looking for new friends, and for new members of the Scripting Guys group. <\/P>\n<P>So how do you sign up? Well, in order to join the group, you first need to go to Facebook.com and join Facebook itself. Admittedly, that involves creating a personal profile, but don\u2019t worry: you can keep that profile totally private if you prefer. After joining Facebook, type <I>Scripting Guys<\/I> in the <B>Search<\/B> box and then hit ENTER. When the search results appear, click the <B>Join Group<\/B> link, then click <B>Join<\/B> when the <B>Join Group<\/B> dialog box appears. After that, just sit back and enjoy the fun!<\/P>\n<P>Enjoy <I>what<\/I> fun? Well, OK, to be honest, there\u2019s not much to enjoy at the moment; after all, we just started the group, and it will take a little while for us to figure out what we want to do with the thing. In the meantime, though, you can exchange messages with the Scripting Guys; in addition, we now have a simple and easy way to keep you notified when new and exciting things take place in the Script Center. <\/P>\n<TABLE id=\"EFE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. All right, who said <I>if<\/I> new and exciting things take place in the Script Center? Have you already forgotten about the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2008 Winter Scripting Games<\/B><\/A>? Incidentally, there\u2019s a page for the Scripting Games, too. Leave us an RSVP, and we\u2019ll be sure to send you a reminder before the Games begin.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, we\u2019d be thrilled if everyone out there would join the group, and if you talked all your friends and family into joining as well. As a token of our appreciation, we\u2019ll show you how to write a Windows PowerShell script that can start an application, and then pause until that application terminates. In fact, we\u2019ll show you <I>this<\/I> script:<\/P><PRE class=\"codeSample\">[System.Diagnostics.Process]::Start(&#8220;notepad&#8221;).WaitForExit()\nWrite-Host &#8220;Notepad has terminated.&#8221;\n<\/PRE>\n<P>As you can see, there\u2019s not much to this thing. (Which, of course, is one of the beauties of Windows PowerShell: you can often carry out tasks using just a couple lines of code). In line one, we\u2019re using the .NET Framework class <B>System.Diagnostics.Process<\/B> to start Notepad; in addition, we\u2019ve tacked the <B>WaitForExit<\/B> method onto the start call. Why? Because WaitForExit instructs PowerShell to simply sit there and wait until the process we just started has terminated. As long as that instance of Notepad remains up and running, our PowerShell script will do nothing at all. The moment Notepad terminates, however, the script resumes with the next line. Which, in this case, simply echoes back the fact that Notepad has ended.<\/P>\n<P>Alternatively, you could also perform this same task using code like this:<\/P><PRE class=\"codeSample\">Notepad | Out-Null\nWrite-Host &#8220;Notepad has terminated.&#8221;\n<\/PRE>\n<P>That works equally well, but it\u2019s not very intuitive; that\u2019s why we used the .NET Framework approach instead. Besides, it never hurts to learn as much as you can about the .NET Framework and what you can do with it.<\/P>\n<P>Here\u2019s another variation on starting a process and then waiting around for awhile before resuming the script. In the following bit of code, we start the Windows Calculator, then pass the value 10000 to the WaitForExit method:<\/P><PRE class=\"codeSample\">[System.Diagnostics.Process]::Start(&#8220;calc&#8221;).WaitForExit(10000)\nWrite-Host &#8220;Calculator has ended, or 10 seconds have elapsed.&#8221;\n<\/PRE>\n<P>What\u2019s the point of that? Well, the 10000 represents 10 seconds (10,000 milliseconds). That means we\u2019re going to start Calculator, wait a maximum 10 seconds, then proceed with the rest of the script. In other words, we start Calculator. If Calculator terminates after a few seconds, we then go ahead and run the next line in the script. Ah, you say, but what if 10 seconds have elapsed and Calculator is still running? In that case, we just let Calculator do its thing and go ahead with the next line in the script anyway.<\/P>\n<P>Give it a try and you\u2019ll see what we mean.<\/P>\n<P>We hope that answers your question, AD, and we hope that answer was good enough to get you to sign up for the Scripting Guys group on Facebook. No doubt a few of you are wondering why we decided to create a Facebook group in the first place. \u201cWe noticed that there\u2019s a TechNet group and it has 45 members.\u201d You point out. \u201cWe also noticed that there\u2019s an MSDN group, and it has 73 members. You guys wouldn\u2019t be doing this just so you could try and get more members in <I>your<\/I> group than in the TechNet and MSDN groups combined, would you?\u201d<\/P>\n<P>Please; do you really think the Scripting Guys would be that shallow and that competitive? Well, you\u2019re right: we <I>are<\/I> that shallow and that competitive. Admittedly, we <I>do<\/I> think that this group gives us some new and interesting ways to interact with our readers. But, hey, if we can kick a little TechNet and MSDN butt while we\u2019re at it, well, that\u2019s just the frosting on the cake.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In Windows PowerShell, how can I start another application, then have my script pause until that application has terminated?&#8212; AD Hey, AD. Before we answer today\u2019s question, we\u2019d like to make an announcement: the Scripting Guys need friends! No, not those kind of friends. Well, come to think of it, we could [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,87,3,45],"class_list":["post-63473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In Windows PowerShell, how can I start another application, then have my script pause until that application has terminated?&#8212; AD Hey, AD. Before we answer today\u2019s question, we\u2019d like to make an announcement: the Scripting Guys need friends! No, not those kind of friends. Well, come to think of it, we could [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=63473"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63473\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=63473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}