{"id":66463,"date":"2006-09-15T15:46:00","date_gmt":"2006-09-15T15:46:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/15\/how-can-i-tell-if-any-internet-explorer-windows-are-open-to-a-particular-web-site\/"},"modified":"2006-09-15T15:46:00","modified_gmt":"2006-09-15T15:46:00","slug":"how-can-i-tell-if-any-internet-explorer-windows-are-open-to-a-particular-web-site","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-any-internet-explorer-windows-are-open-to-a-particular-web-site\/","title":{"rendered":"How Can I Tell if Any Internet Explorer Windows Are Open to a Particular Web Site?"},"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 determine if I have any Internet Explorer windows open to a particular Web site?<BR><BR>&#8212; N<\/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, N. Before we begin the Scripting Guys would like to state, for the record, that we have never tried to obtain each other\u2019s phone records nor have we ever even <I>considered<\/I> listening in on each other\u2019s phone conversations. This has become an issue in the tech world, and we thought it was important that everyone knows where we stand.<\/P>\n<P>Well, OK, if you want to get technical, at one time we <I>did<\/I> try listening in on <A href=\"http:\/\/null\/technet\/scriptcenter\/sgwho.mspx\"><B>Peter Costantini\u2019s<\/B><\/A> phone calls home. However, seeing as how none of us speak Martian we soon gave up on that. <\/P>\n<P>In fact, not only do we not listen to each other\u2019s phone conversations (some of us barely listen to our <I>own<\/I> phone conversations) but \u2013 and, for once, this is actually relevant to the question you asked \u2013 we typically don\u2019t try to determine if any of us have any browser windows open to a particular Web site. That\u2019s not because we aren\u2019t nosy; we are. That\u2019s simply because this is a difficult task to perform against remote computers. In turn, that\u2019s because the only way we know to do this is to use the Windows Shell object, an object that can only be created \u2013 and used \u2013 on the local machine.<\/P>\n<P>So what does that mean? Well, that means we\u2019ll show you a solution that will work, albeit only on your local computer. That\u2019s fine, except for one thing: what if you want to get this information from a remote machine? In that case we can think of a couple possibilities. For one, you could use the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_wsh_immo.mspx\"><B>WSH Controller<\/B><\/A> object or WMI\u2019s <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0901.mspx\"><B>Win32_Process<\/B><\/A> class to kick off the script on a remote machine. Either of those methods will work, although you <I>will<\/I> have to modify the code so that the results aren\u2019t displayed in a message box but are, instead, saved to a text file or database or something. <\/P>\n<TABLE id=\"EAE\" 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>. Why can\u2019t you display a message box using these methods? Well, you can; it\u2019s just that the message box will pop up on the remote machine.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Alternatively, you might configure this script to run as a scheduled task; that way you could have it run every <I>x<\/I> number of minutes, check to see if any browser windows are open to the specified Web site, and then report back the results. Here, too you\u2019ll have to modify the code so that the results are saved somewhere rather than displayed in a message box. That\u2019s not particularly complicated, just something you\u2019ll have to decide for yourself.<\/P>\n<P>But enough with the excuses. Let\u2019s take a look at a script that can tell us whether or not a browser window is open to a specified Web site:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objShellWindows = objShell.Windows<\/p>\n<p>If objShellWindows.Count = 0 Then\n    Wscript.Echo &#8220;No browser windows are open to the Script Center.&#8221;\n    Wscript.Quit\nEnd If<\/p>\n<p>blnFound = False<\/p>\n<p>For i = 0 to objShellWindows.Count &#8211; 1\n    Set objIE = objShellWindows.Item(i)\n    strURL = objIE.LocationURL\n    If InStr(strURL, &#8220;http:\/\/www.microsoft.com\/technet\/scriptcenter&#8221;)Then\n        blnFound = True\n    End If\nNext<\/p>\n<p>If blnFound Then\n    Wscript.Echo &#8220;At least one browser window is open to the Script Center.&#8221;\nElse\n    Wscript.Echo &#8220;No browser windows are open to the Script Center.&#8221;\nEnd If\n<\/PRE>\n<P>Just like we said we would, we start off by creating an instance of the <B>Shell.Application<\/B> object. With that object in tow we then use the following line of code to return a collection of all the Shell windows currently open:<\/P><PRE class=\"codeSample\">Set objShellWindows = objShell.Windows\n<\/PRE>\n<P>In case you\u2019re wondering, Shell windows are, well, windows that belong to the Shell. If you have Windows Explorer or a My Computer window open then you have at least Shell window open. The same is true for Internet Explorer: a running instance of Internet Explorer will appear in the Shell\u2019s <B>Windows<\/B> collection. <\/P>\n<P>Which, needless to say, is why we grabbed this collection in the first place.<\/P>\n<P>Our next step is to check the value of the collection\u2019s <B>Count<\/B> property; this tells us how many Shell windows are currently open. Why do we bother doing that? Well, if the Count is 0 that means there aren\u2019t <I>any<\/I> Shell windows open; if no Shell windows are open then it\u2019s a pretty good bet that there aren\u2019t any Shell windows open to a particular Web site. Therefore, if the Count is 0 we echo back the fact that no browser windows are open to the target Web site, then use the <B>Quit<\/B> method to terminate the script. That\u2019s what this block of code is for:<\/P><PRE class=\"codeSample\">If objShellWindows.Count = 0 Then\n    Wscript.Echo &#8220;No browser windows are open to the Script Center.&#8221;\n    Wscript.Quit\nEnd If\n<\/PRE>\n<P>Cool. But what if the Count <I>isn\u2019t<\/I> 0? What then?<\/P>\n<P>Well, after setting a variable named blnFound to False (more on that momentarily) we set up a For Next loop that runs from 0 to the collection Count minus 1:<\/P><PRE class=\"codeSample\">For i = 0 to objShellWindows.Count &#8211; 1\n<\/PRE>\n<TABLE id=\"ERF\" 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>. We knew you were going to ask that: why <I>does<\/I> the loop run from 0 to the Count minus 1? That\u2019s because our collection operates just like an array: the first item in the collection has an index number (subscript) of 0, while the last item has an index number equal to the total number of items minus 1. If we have 9 items in the collection, the index number of the last item will be 8 (9 \u2013 1). Thus we start at 0 (the first item) and continue through the Count minus 1 (the last item).<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Inside the loop we use this line of code to bind to the current item in the collection:<\/P><PRE class=\"codeSample\">Set objIE = objShellWindows.Item(i)\n<\/PRE>\n<P>As you no doubt know, the first time through the loop the counter variable i is equal to 0; that means that this code will bind us to item 0, the first item in the collection.<\/P>\n<P>After making the connection we store the value of the <B>LocationURL<\/B> property in the variable strURL. We then use VBScript\u2019s <B>InStr<\/B> function to determine whether or not the window is open to the target Web site (which is what the LocationURL property tells us). For example, if our target Web site is the Script Center, we know that any page in the Script Center has to start with the string <I>http:\/\/www.microsoft.com\/technet\/scriptcenter<\/I>. Consequently, we use InStr to determine whether that string appears anywhere in the LocationURL:<\/P><PRE class=\"codeSample\">If InStr(strURL, &#8220;http:\/\/www.microsoft.com\/technet\/scriptcenter&#8221;)Then\n        blnFound = True\n<\/PRE>\n<P>If the target string is found (meaning InStr returned a value other than 0) we then set the value of the variable blnFound to True; that way we\u2019ll know that we found at least one window open to the Script Center. If the target string is not found then we leave blnFound alone. Either way, we then loop around and check the next item in the collection.<\/P>\n<P>That brings us to our final block of code:<\/P><PRE class=\"codeSample\">If blnFound Then\n    Wscript.Echo &#8220;At least one browser window is open to the Script Center.&#8221;\nElse\n    Wscript.Echo &#8220;No browser windows are open to the Script Center.&#8221;\nEnd If\n<\/PRE>\n<P>Here we\u2019re checking the value of blnFound. If blnFound is True, that means that, somewhere along the line, we stumbled upon a browser window open to the Script Center; in response, we echo back that fact. If blnFound is False, that can only mean one thing: we didn\u2019t find <I>any<\/I> browser windows open to the Script Center. (Remember the value of blnFound starts out as False, and changes only if we find the target Web site.) In turn, we then echo back the fact that we didn\u2019t find any browser windows open to the Script Center.<\/P>\n<P>Like we said, N, this might not be the ultimate solution for you; however, it should be enough to get you started. (And it might be exactly what you need if you plan on running this script locally.) If this doesn\u2019t quite answer your question just let us know. We\u2019ll either try to come up with a better solution for you or we\u2019ll just listen in on one of <A href=\"http:\/\/null\/technet\/scriptcenter\/sgwho.mspx\"><B>Dean\u2019s<\/B><\/A> phone conversations and see if <I>he<\/I> can come up with a better solution for you.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine if I have any Internet Explorer windows open to a particular Web site?&#8212; N Hey, N. Before we begin the Scripting Guys would like to state, for the record, that we have never tried to obtain each other\u2019s phone records nor have we ever even considered listening in [&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,17,3,707,5],"class_list":["post-66463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-internet-explorer","tag-scripting-guy","tag-shell-application","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine if I have any Internet Explorer windows open to a particular Web site?&#8212; N Hey, N. Before we begin the Scripting Guys would like to state, for the record, that we have never tried to obtain each other\u2019s phone records nor have we ever even considered listening in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66463","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=66463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66463\/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=66463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}