{"id":68433,"date":"2005-11-30T13:38:00","date_gmt":"2005-11-30T13:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/30\/how-can-i-close-a-command-window-with-a-specific-title\/"},"modified":"2005-11-30T13:38:00","modified_gmt":"2005-11-30T13:38:00","slug":"how-can-i-close-a-command-window-with-a-specific-title","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-close-a-command-window-with-a-specific-title\/","title":{"rendered":"How Can I Close a Command Window with a Specific Title?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! How can I use WSH to close a command window with a specific title?<BR><BR>&#8212; MF<\/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, 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\u2019s crazy, but physicists insist it\u2019s true. The University of Washington football team has won only 3 games in the past 2 years? That\u2019s even <I>crazier<\/I>. But, as Casey Stengal said, you can look it up. <\/P>\n<TABLE id=\"E3C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Editor\u2019s Note<\/B>. Want to hear something <I>really<\/I> 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\u2019d know how incredible it is that one has survived a whole year. Actually, from what you do know of the Scripting Guys, you\u2019re probably wondering how <I>anybody<\/I> could work with them a whole year. I know this particular editor is wondering\u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Or how about this one: you <I>can\u2019t<\/I> 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\u2019t close a specific command window, at least not based on window title alone.<\/P>\n<P>Of course, there\u2019s one thing that <I>is<\/I> easy to believe: any time the Scripting Guys tell you that something can\u2019t be done the odds are they\u2019ll come up with some crazy workaround that lets you do the task anyway. This is no exception. Maybe you can\u2019t use WSH to close a command window with a specific title; however, you <I>can<\/I> use Microsoft Word to close a command window with a specific title.<\/P>\n<P>Believe it or not.<\/P>\n<P>For example, here\u2019s a script that closes a command window with the title <B>My Window<\/B>:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nSet colTasks = objWord.Tasks<\/p>\n<p>If colTasks.Exists(&#8220;My Window&#8221;) Then\n    colTasks(&#8220;My Window&#8221;).Close\nEnd If<\/p>\n<p>objWord.Quit\n<\/PRE>\n<P>As you can see, the script begins by creating an instance of the <B>Word.Application<\/B> object. (See? We <I>told<\/I> 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:<\/P><PRE class=\"codeSample\">Set colTasks = objWord.Tasks\n<\/PRE>\n<TABLE id=\"EKE\" 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>. You might have noticed that, unlike most of the Word scripts we use, we never set the <B>Visible<\/B> 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\u2019t want &#8211; or need &#8211; Word to show up onscreen; we just need it to close a command window for us. So we keep it hidden.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So what\u2019s the difference between the <B>Tasks<\/B> collection in Word and the set of processes returned by WMI\u2019s Win32_Process class? The primary difference is that Word returns the \u201cfriendly name\u201d for each process. WMI returns the name of the executable file, so you get back information like this:<\/P><PRE class=\"codeSample\">winword.exe\nsol.exe\ncmd.exe\n<\/PRE>\n<P>With Word, though, you get back information that looks like this:<\/P><PRE class=\"codeSample\">Test.doc &#8211; Microsoft Word\nSolitaire\nMy Window\n<\/PRE>\n<P>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\u2019t be true for processes that run in hidden windows, but we won\u2019t worry about those today.) As you can see, our command window is returned as My Window and <I>not<\/I> as the generic Cmd.exe. What does that mean? That means Word &#8211; unlike WSH and WMI &#8211; can locate specific command windows by title.<\/P>\n<P>Furthermore, Word can also close those command windows. After returning the collection of tasks running on the computer we use the <B>Exists<\/B> method to determine whether or not there is a process (or task) named My Window. If there is, we call the <B>Close<\/B> method and terminate that process. That takes just three lines of code:<\/P><PRE class=\"codeSample\">If colTasks.Exists(&#8220;My Window&#8221;) Then\n    colTasks(&#8220;My Window&#8221;).Close\nEnd If\n<\/PRE>\n<P>All we have to do then is use the <B>Quit<\/B> method to terminate our invisible instance of Word and we\u2019re done.<\/P>\n<P>Yes, we know it\u2019s hard to believe (though less hard than believing that <I>Shrek<\/I> would turn out to be one of the most popular movies of all time). But it\u2019s all true. For more information about the Tasks collection in Word (and what you can do with) take a look at the article <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/office\/tasks.mspx\"><B>Build Your Own Task Manager Using Microsoft Word (No, Really)<\/B><\/A>.<\/P>\n<P>And, sadly, it <I>is<\/I> 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!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use WSH to close a command window with a specific title?&#8212; MF 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 [&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":[16,84,49,3,5,395],"class_list":["post-68433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use WSH to close a command window with a specific title?&#8212; MF 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68433","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=68433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68433\/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=68433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}