{"id":70423,"date":"2005-02-16T16:49:00","date_gmt":"2005-02-16T16:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/16\/how-can-i-list-open-sessions-and-open-files-on-a-computer\/"},"modified":"2005-02-16T16:49:00","modified_gmt":"2005-02-16T16:49:00","slug":"how-can-i-list-open-sessions-and-open-files-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-open-sessions-and-open-files-on-a-computer\/","title":{"rendered":"How Can I List Open Sessions and Open Files on a Computer?"},"content":{"rendered":"<p><P>&nbsp;<\/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! Is there a way to export the information found in the Shared Folders portion of the Computer Management snap-in (in particular, the list of sessions and the list of open files)?<BR><BR>&#8212; PP<\/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, PP. Funny you should ask; this very issue came up the other day, and while we assumed that there was a way to get at this information we weren\u2019t totally sure about that. As it turns out, though, there are actually a couple different ways to get at this data; for now we\u2019ll show you one approach that can retrieve this information off of Windows 2000 computers as well as Windows XP and Windows 2003 computers.<\/P>\n<P>So how do we get at the sessions as shown in the Shared Folders snap-in? One way is to use the IADsSession interface. That sounds very technical, but it\u2019s actually pretty easy. For example, here\u2019s a script that binds to the file service on the computer atl-ws-01 and then echoes back information about each session. Note that in our binding string we connect to <B>LanmanServer<\/B>; that\u2019s because &#8211; in the registry &#8211; LanmanServer is the name used by the file service. Here\u2019s the script:<\/P><PRE class=\"codeSample\">Set objConnection = GetObject(&#8220;WinNT:\/\/atl-ws-01\/LanmanServer&#8221;)\nSet colSessions = objConnection.Sessions<\/p>\n<p>For Each objSession in colSessions\n    Wscript.Echo &#8220;Computer: &#8221; &amp; objSession.Computer\n    Wscript.Echo &#8220;Connected Time: &#8221; &amp; objSession.ConnectTime\n    Wscript.Echo &#8220;Idle Time: &#8221; &amp; objSession.IdleTime\n    Wscript.Echo &#8220;Name: &#8221; &amp; objSession.Name\n    Wscript.Echo &#8220;User: &#8221; &amp; objSession.User\n    Wscript.Echo\nNext\n<\/PRE>\n<P>We begin by binding to the file service; after making the connection the script then retrieves a collection of open sessions. That\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">Set colSessions = objConnection.Sessions\n<\/PRE>\n<P>The rest of the script is simply a matter of walking through that collection and echoing back the property values for each open session. (To tell you the truth, we thought it was going to be harder than that, too; like we said, this is almost <I>too<\/I> easy.)<\/P>\n<P>Incidentally, if you want to verify that the script is returning the correct data, type <B>net sessions<\/B> at the command prompt; the data brought back by your script and the data brought back by Net.exe should be the same. Likewise, you can verify that your script is returning the correct information about open files by typing <B>net files<\/B> from the command prompt.<\/P>\n<P>Oh, yeah: what <I>about<\/I> open files? Turns out we can use the IADsResource interface to get the set of open files:<\/P><PRE class=\"codeSample\">Set objConnection = GetObject(&#8220;WinNT:\/\/atl-ws-01\/LanmanServer&#8221;)\nSet colResources = objConnection.Resources<\/p>\n<p>For Each objResource in colResources\n    Wscript.Echo &#8220;Path: &#8221; &amp; objResource.Path\n    Wscript.Echo &#8220;User: &#8221; &amp; objResource.User\n    Wscript.Echo\nNext\n<\/PRE>\n<P>As you can see, this is very similar to the script that returns the open sessions; the only differences are: 1) We retrieve a collection of resources rather than sessions; and, 2) We echo a different set of property values. But that makes sense: after all, open files are going to have different properties and property values than open sessions. There you go: open files and open sessions. <\/P>\n<P>Of course, one thing we\u2019ve learned in writing this column is that the moment we answer one question people respond with 50 or 60 additional questions related to that topic. Because of that we know we\u2019re bound to get questions like, \u201cHow can I disconnect all these sessions?\u201d or \u201cHow can I close all these files?\u201d Without going in any detail, here\u2019s a sample script that closes all the open sessions on a computer:<\/P><PRE class=\"codeSample\">Set objConnection = GetObject(&#8220;WinNT:\/\/atl-ws-01\/LanmanServer&#8221;)\nSet colSessions = objConnection.Sessions<\/p>\n<p>For Each objSession in colSessions\n    colSessions.Remove(objSession.Name)\nNext\n<\/PRE>\n<P>And here\u2019s one that closes all the open files:<\/P><PRE class=\"codeSample\">Set objConnection = GetObject(&#8220;WinNT:\/\/atl-ws-01\/LanmanServer&#8221;)\nSet colResources = objConnection.Resources<\/p>\n<p>For Each objResource in colResources\n    colResources.Remove(objResource.Name)\nNext\n<\/PRE>\n<P>We should point out that when we tested these close session and close file scripts they seemed to work just fine on Windows 2000 Server and Windows Server 2003. Results on Windows XP, however, were mixed at best. So, no guarantees; give them a try and see if they work in your environment.<\/P><BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! Is there a way to export the information found in the Shared Folders portion of the Computer Management snap-in (in particular, the list of sessions and the list of open files)?&#8212; PP Hey, PP. Funny you should ask; this very issue came up the other day, and while we assumed that [&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":[31,3,709,5],"class_list":["post-70423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scripting-guy","tag-sessions","tag-vbscript"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! Is there a way to export the information found in the Shared Folders portion of the Computer Management snap-in (in particular, the list of sessions and the list of open files)?&#8212; PP Hey, PP. Funny you should ask; this very issue came up the other day, and while we assumed that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70423","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=70423"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70423\/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=70423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}