{"id":67773,"date":"2006-03-13T22:28:00","date_gmt":"2006-03-13T22:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/13\/how-can-i-write-output-to-the-screen-that-overwrites-whatever-is-currently-on-the-screen\/"},"modified":"2006-03-13T22:28:00","modified_gmt":"2006-03-13T22:28:00","slug":"how-can-i-write-output-to-the-screen-that-overwrites-whatever-is-currently-on-the-screen","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-write-output-to-the-screen-that-overwrites-whatever-is-currently-on-the-screen\/","title":{"rendered":"How Can I Write Output to the Screen that Overwrites Whatever is Currently on the Screen?"},"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 write output to the screen that overwrites whatever is currently on the screen?<BR><BR>&#8212; KM<\/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, KM. If you specifically need to write your output to the command window, well, then we don\u2019t have an answer for you: although we played around with a few things, we never found an easy, straightforward way to overwrite information in the command window.<\/P>\n<P>However, if you\u2019re willing to output your information to an Internet Explorer window, well, then we <I>do<\/I> have an answer for you. And, coincidentally, here\u2019s that answer right now:<\/P><PRE class=\"codeSample\">Set objExplorer = CreateObject(&#8220;InternetExplorer.Application&#8221;)\nobjExplorer.Navigate &#8220;about:blank&#8221;   \nobjExplorer.ToolBar = 0\nobjExplorer.StatusBar = 0\nobjExplorer.Width = 400\nobjExplorer.Height = 200 \nobjExplorer.Left = 0\nobjExplorer.Top = 0<\/p>\n<p>Do While (objExplorer.Busy)\n    Wscript.Sleep 200\nLoop    <\/p>\n<p>objExplorer.Document.Title = &#8220;Process Information&#8221;   \nobjExplorer.Visible = 1  <\/p>\n<p>objExplorer.Document.Body.InnerHTML = &#8220;Retrieving process information.&#8221; <\/p>\n<p>Wscript.Sleep 2000<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_Process&#8221;)\nFor Each objItem in colItems\n    objExplorer.Document.Body.InnerHTML = objItem.Name\n    Wscript.Sleep 500\nNext<\/p>\n<p>objExplorer.Document.Body.InnerHTML = &#8220;Process information retrieved.&#8221;\nWscript.Sleep 3000\nobjExplorer.Quit\n<\/PRE>\n<P>Granted this looks a little long, but it\u2019s actually pretty easy, as you\u2019re about to find out. For example, at first glance this block of code might look a bit intimidating:<\/P><PRE class=\"codeSample\">Set objExplorer = CreateObject(&#8220;InternetExplorer.Application&#8221;)\nobjExplorer.Navigate &#8220;about:blank&#8221;   \nobjExplorer.ToolBar = 0\nobjExplorer.StatusBar = 0\nobjExplorer.Width = 400\nobjExplorer.Height = 200 \nobjExplorer.Left = 0\nobjExplorer.Top = 0\n<\/PRE>\n<P>As it turns out, though, all we\u2019re doing here is creating a blank instance of Internet Explorer; in particular, that\u2019s what the first two lines of code do. The remaining lines of code simply configure various properties of the Internet Explorer window: we set the width to 400 pixels, we hide the tool bar, and we position the Internet Explorer window in the upper left-hand corner of the screen. If you\u2019re happy with the default configuration of the Internet Explorer window then you could skip lines 3 through 8. <\/P>\n<P>After we\u2019re done setting up our Internet Explorer window we use this Do While loop to pause the script until Internet Explorer is fully loaded:<\/P><PRE class=\"codeSample\">Do While (objExplorer.Busy)\n    Wscript.Sleep 200\nLoop\n<\/PRE>\n<P>See? That\u2019s no so bad. With Internet Explorer up and running we next give our Internet Explorer window a title and set the <B>Visible<\/B> property to True (1). We need to do that because, up till now, Internet Explorer has been running in a hidden window and has not been visible on screen. But this code will take care of that little problem:<\/P><PRE class=\"codeSample\">objExplorer.Document.Title = &#8220;Process Information&#8221;   \nobjExplorer.Visible = 1\n<\/PRE>\n<P>Now we\u2019re ready to write something to our window. For this sample script that\u2019s as easy as this:<\/P><PRE class=\"codeSample\">objExplorer.Document.Body.InnerHTML = &#8220;Retrieving process information.&#8221;\n<\/PRE>\n<P>As you can see, we\u2019re simply assigning a value to the <B>Document.Body<\/B> object\u2019s<B> InnerHTML<\/B> property. In this case we\u2019re merely assigning InnerHTML some text: <I>Retrieving process information<\/I>. However, we could easily add some HTML tags and create fancier output. For example, this line of code will boldface the text written to the window:<\/P><PRE class=\"codeSample\">objExplorer.Document.Body.InnerHTML = &#8220;&lt;B&gt;Retrieving process information.&lt;\/B&gt;&#8221;\n<\/PRE>\n<P>OK, maybe that doesn\u2019t qualify as <I>fancy<\/I>. But you get the idea.<\/P>\n<P>After displaying our text string in the Internet Explorer window we then pause the script for 2 seconds (2000 milliseconds). There\u2019s no reason why we <I>have<\/I> to do that; that\u2019s done simply so you\u2019ll have a chance to see the text before we overwrite it. <\/P>\n<P>That brings us to this block of code:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_Process&#8221;)\nFor Each objItem in colItems\n    objExplorer.Document.Body.InnerHTML = objItem.Name\n    Wscript.Sleep 500\nNext\n<\/PRE>\n<P>All we\u2019re doing here is retrieving a collection of the processes running on the local computer. That\u2019s not important; we just need the script to do <I>something<\/I> interesting. What we care about today is what happens inside the For Each loop we set up to walk through all the items in that collection:<\/P><PRE class=\"codeSample\">For Each objItem in colItems\n    objExplorer.Document.Body.InnerHTML = objItem.Name\n    Wscript.Sleep 500\nNext\n<\/PRE>\n<P>In most WMI scripts we\u2019d echo back property values (such as <B>Name<\/B>) inside our For Each loop. Here, however, we don\u2019t use Wscript.Echo. Instead, we assign the name of the first process in the collection to the body\u2019s InnerHTML property:<\/P><PRE class=\"codeSample\">objExplorer.Document.Body.InnerHTML = objItem.Name\n<\/PRE>\n<P>What\u2019s that going to do? That\u2019s going to overwrite the existing contents of our Internet Explorer window with the name of the first process in the collection. For example, at the time we begin the loop the Internet Explorer window will contain this text:<\/P>\n<P>Retrieving process information.<\/P>\n<P>As we start the loop, that text will be replaced with the name of the first process in the collection. For example:<\/P>\n<P>Winword.exe.<\/P>\n<P>After overwriting the contents of the window we pause the script for half a second (500 milliseconds). Again, that\u2019s not required; we do that simply to slow the script down a little and give you a chance to see the first process name displayed. Half a second later we loop around and retrieve information about the second process in the collection. The script then replaces the existing contents of the Internet Explorer window (technically, the value assigned to the InnerHTML property) with the name of the second process. Etc. etc.<\/P>\n<P>After we\u2019ve looped through the entire collection we indicate that the script has finished, pause for 3 seconds, and then dismiss the Internet Explorer window:<\/P><PRE class=\"codeSample\">objExplorer.Document.Body.InnerHTML = &#8220;Process information retrieved.&#8221;\nWscript.Sleep 3000\nobjExplorer.Quit\n<\/PRE>\n<P>This works pretty good, and is pretty easy as well. And because it uses HTML that means you can make your output as fancy as you want; in fact, you could even make your Internet Explorer window look like a command window. If that\u2019s not cool, well, we don\u2019t know what is. (Actually, as one of the Scripting Sons constantly reminds his Scripting Father, we <I>don\u2019t<\/I> know what cool is.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I write output to the screen that overwrites whatever is currently on the screen?&#8212; KM Hey, KM. If you specifically need to write your output to the command window, well, then we don\u2019t have an answer for you: although we played around with a few things, we never found an [&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":[17,31,87,3,167,5],"class_list":["post-67773","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-internet-explorer","tag-operating-system","tag-processes","tag-scripting-guy","tag-using-the-internet","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I write output to the screen that overwrites whatever is currently on the screen?&#8212; KM Hey, KM. If you specifically need to write your output to the command window, well, then we don\u2019t have an answer for you: although we played around with a few things, we never found an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67773","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=67773"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67773\/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=67773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}