Hey, Scripting Guy! How Can I Start an Application From a Windows PowerShell Script, Then Pause the Script Until the Application Has Terminated?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! In Windows PowerShell, how can I start another application, then have my script pause until that application has terminated?

— AD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AD. Before we answer today’s question, we’d like to make an announcement: the Scripting Guys need friends!

No, not those kind of friends. Well, come to think of it, we could use some regular friends, too. For now, however, we’re specifically looking for some new Facebook friends. Just this morning we created a Facebook page for the Scripting Guys, but – alas – we don’t 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):

Spacer


Now, we aren’t saying that Jean and Greg aren’t a lot of fun, but – well, OK, so maybe we are saying that Jean and Greg aren’t a lot of fun. (Greg’s actually OK, but Jean looks like a question mark, for Pete’s sake!) That’s why we’re looking for new friends, and for new members of the Scripting Guys group.

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’t worry: you can keep that profile totally private if you prefer. After joining Facebook, type Scripting Guys in the Search box and then hit ENTER. When the search results appear, click the Join Group link, then click Join when the Join Group dialog box appears. After that, just sit back and enjoy the fun!

Enjoy what fun? Well, OK, to be honest, there’s 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.

Note. All right, who said if new and exciting things take place in the Script Center? Have you already forgotten about the 2008 Winter Scripting Games? Incidentally, there’s a page for the Scripting Games, too. Leave us an RSVP, and we’ll be sure to send you a reminder before the Games begin.

Anyway, we’d 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’ll show you how to write a Windows PowerShell script that can start an application, and then pause until that application terminates. In fact, we’ll show you this script:

[System.Diagnostics.Process]::Start(“notepad”).WaitForExit()
Write-Host “Notepad has terminated.”

As you can see, there’s 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’re using the .NET Framework class System.Diagnostics.Process to start Notepad; in addition, we’ve tacked the WaitForExit 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.

Alternatively, you could also perform this same task using code like this:

Notepad | Out-Null
Write-Host “Notepad has terminated.”

That works equally well, but it’s not very intuitive; that’s 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.

Here’s 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:

[System.Diagnostics.Process]::Start(“calc”).WaitForExit(10000)
Write-Host “Calculator has ended, or 10 seconds have elapsed.”

What’s the point of that? Well, the 10000 represents 10 seconds (10,000 milliseconds). That means we’re 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.

Give it a try and you’ll see what we mean.

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. “We noticed that there’s a TechNet group and it has 45 members.” You point out. “We also noticed that there’s an MSDN group, and it has 73 members. You guys wouldn’t be doing this just so you could try and get more members in your group than in the TechNet and MSDN groups combined, would you?”

Please; do you really think the Scripting Guys would be that shallow and that competitive? Well, you’re right: we are that shallow and that competitive. Admittedly, we do 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’re at it, well, that’s just the frosting on the cake.

0 comments

Discussion is closed.

Feedback usabilla icon