How Can I Close a Command Window with a Specific Title?

How Can I Close a Command Window with a Specific Title?

  • Comments 2
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I use WSH to close a command window with a specific title?

-- MF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MF. You know, some things are just really hard to believe, regardless of whether or not they happen to be true. For example, according to the Big Bang Theory the entire universe was created when a miniscule spec of super-dense matter exploded several billion years ago. That’s crazy, but physicists insist it’s true. The University of Washington football team has won only 3 games in the past 2 years? That’s even crazier. But, as Casey Stengal said, you can look it up.

Editor’s Note. Want to hear something really crazy? Today is the one-year anniversary of the Scripting Guys Editor joining the team. If you knew the history of the Scripting Guys and editors, you’d know how incredible it is that one has survived a whole year. Actually, from what you do know of the Scripting Guys, you’re probably wondering how anybody could work with them a whole year. I know this particular editor is wondering….

Or how about this one: you can’t use WSH (or WMI, for that matter) to close a command window with a specific title. WMI enables you to close command windows (or, more correctly, it enables you to terminate processes running under Cmd.exe) but WMI has no knowledge of command window titles. If all you have to go on is the command window title WMI becomes an all-or-nothing proposition: you can either close all the command windows (that is, all processes running under Cmd.exe) or you can close none of the command windows. But you can’t close a specific command window, at least not based on window title alone.

Of course, there’s one thing that is easy to believe: any time the Scripting Guys tell you that something can’t be done the odds are they’ll come up with some crazy workaround that lets you do the task anyway. This is no exception. Maybe you can’t use WSH to close a command window with a specific title; however, you can use Microsoft Word to close a command window with a specific title.

Believe it or not.

For example, here’s a script that closes a command window with the title My Window:

Set objWord = CreateObject("Word.Application")
Set colTasks = objWord.Tasks

If colTasks.Exists("My Window") Then
    colTasks("My Window").Close
End If

objWord.Quit

As you can see, the script begins by creating an instance of the Word.Application object. (See? We told you you could use Microsoft Word to carry out this task!) We then use this line of code to return a collection of all the tasks (processes) running on a computer:

Set colTasks = objWord.Tasks

Note. You might have noticed that, unlike most of the Word scripts we use, we never set the Visible property to True in this script. Why not? Well, setting the Visible property to True causes Word to appear onscreen. In this case we don’t want - or need - Word to show up onscreen; we just need it to close a command window for us. So we keep it hidden.

So what’s the difference between the Tasks collection in Word and the set of processes returned by WMI’s Win32_Process class? The primary difference is that Word returns the “friendly name” for each process. WMI returns the name of the executable file, so you get back information like this:

winword.exe
sol.exe
cmd.exe

With Word, though, you get back information that looks like this:

Test.doc - Microsoft Word
Solitaire
My Window

Generally speaking, the task name returned by Word will be the name that appears in the title bar of the application window. (That obviously won’t be true for processes that run in hidden windows, but we won’t worry about those today.) As you can see, our command window is returned as My Window and not as the generic Cmd.exe. What does that mean? That means Word - unlike WSH and WMI - can locate specific command windows by title.

Furthermore, Word can also close those command windows. After returning the collection of tasks running on the computer we use the Exists method to determine whether or not there is a process (or task) named My Window. If there is, we call the Close method and terminate that process. That takes just three lines of code:

If colTasks.Exists("My Window") Then
    colTasks("My Window").Close
End If

All we have to do then is use the Quit method to terminate our invisible instance of Word and we’re done.

Yes, we know it’s hard to believe (though less hard than believing that Shrek would turn out to be one of the most popular movies of all time). But it’s all true. For more information about the Tasks collection in Word (and what you can do with) take a look at the article Build Your Own Task Manager Using Microsoft Word (No, Really).

And, sadly, it is true: the Husky football team has only won 3 games in the past two years. But the basketball team is already 6-0 this season. Go Dawgs!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I tried the same code for excel application by creating objects to excel. but its throwing error like this property is not supported.
    How can i use this code to close excel application for specific title?

  • thanks