{"id":67573,"date":"2006-04-10T10:11:00","date_gmt":"2006-04-10T10:11:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/10\/how-can-i-list-all-the-files-on-a-users-desktop\/"},"modified":"2006-04-10T10:11:00","modified_gmt":"2006-04-10T10:11:00","slug":"how-can-i-list-all-the-files-on-a-users-desktop","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-files-on-a-users-desktop\/","title":{"rendered":"How Can I List All the Files on a User\u2019s Desktop?"},"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 list all the files on a user\u2019s desktop?<BR><BR>&#8212; BT<\/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, BT. So you thought you could fool the Scripting Guys, did you? Well, you did, at least for awhile. As is so often the case with the Scripting Guys we took one look at your question and thought, \u201cLet\u2019s answer this one; this one is <I>easy<\/I>!\u201d Three tries &#8211; and another false start or two &#8211; later, we <I>finally<\/I> came up with an answer for you. So maybe it wasn\u2019t that easy after all (although, for the Scripting Guys, it was fairly typical).<\/P>\n<P>Why did we struggle with this particular question? Well, setting aside the fact that there are days (many of them) in which we struggle with just about <I>everything<\/I>, we made a couple of misguided assumptions early on. For one, we assumed that connecting to the user\u2019s desktop folder would return us all the information we needed. With that in mind we wrote a script similar to this:<\/P><PRE class=\"codeSample\">Const DESKTOP = &amp;H10&amp;<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(DESKTOP)\nSet objFolderItem = objFolder.Self<\/p>\n<p>Set colItems = objFolder.Items\nFor Each objItem in colItems\n    Wscript.Echo objItem.Name\nNext\n<\/PRE>\n<P>Did this script work? Pretty much, although it also shows you the folders on the desktop as well as the files (we never got around to filtering out folders). Unfortunately, though, it didn\u2019t show us <I>all<\/I> the files in the user\u2019s desktop. Why not? Well, as <I>you<\/I> probably know, the items that appear on the desktop are actually drawn from two places: the user\u2019s Desktop folder and the All Users Desktop folder. Our initial try at solving this problem retrieved the files from only the user\u2019s desktop folder.<\/P>\n<P>Well, if at first you don\u2019t succeed, right? And so we added this code to the original script:<\/P><PRE class=\"codeSample\">Const ALL_USERS_DESKTOP = &amp;H19&amp;<\/p>\n<p>Set objFolder = objShell.Namespace(ALL_USERS_DESKTOP)\nSet objFolderItem = objFolder.Self<\/p>\n<p>Set colItems = objFolder.Items\nFor Each objItem in colItems\n    Wscript.Echo objItem.Name\nNext\n<\/PRE>\n<P>Did <I>that<\/I> script work? You bet it did: now we could get back a list of all the files that appear on the user\u2019s desktop.<\/P>\n<P>Well, check that: all the files that appear on the <I>local<\/I> user\u2019s desktop. What if we needed to get this information for a remote machine? That was a problem: we used the Shell object in our script, and the Shell object doesn\u2019t do us much good when working with remote computers.<\/P>\n<P>At that point we were ready to give up and tell you that we could list the desktop files only for the local user, when it occurred to us that you can find the paths to the Desktop and All Users Desktop folders in the registry. That implied that we could read these values from the registry on a remote computer, and then use WMI to return the list of files. Will that really work? Well, it seems to:<\/P><PRE class=\"codeSample\">Const HKEY_LOCAL_MACHINE = &amp;H80000002\nConst HKEY_CURRENT_USER = &amp;H80000001<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objRegistry=GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders&#8221;\nstrValueName = &#8220;Common Desktop&#8221;\nobjRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strAllUsers<\/p>\n<p>Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name='&#8221; &amp; strAllUsers &amp; &#8220;&#8216;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFileList\n    Wscript.Echo objFile.Name\nNext<\/p>\n<p>Wscript.Echo<\/p>\n<p>strKeyPath = &#8220;Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders&#8221;\nstrValueName = &#8220;Desktop&#8221;\nobjRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strUser<\/p>\n<p>Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name='&#8221; &amp; strUser &amp; &#8220;&#8216;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFileList\n    Wscript.Echo objFile.Name\nNext\n<\/PRE>\n<P>Don\u2019t be intimidated by the length of this script: it\u2019s a little longer than most <I>Hey, Scripting Guy!<\/I> scripts simply because we need to access two different registry keys and then retrieve the list of files from two different folders. As you\u2019re about to see, there\u2019s nothing the least bit complicated about the script.<\/P>\n<P>We start off by defining a pair of constants: HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER. We\u2019ll use these constants to connect to the appropriate registry hives: we use HKEY_LOCAL_MACHINE to get information about the All Users Desktop folder and HKEY_CURRENT_USER to get information about the current user\u2019s Desktop folder. We then use the following two lines of code to connect to the WMI service on the local computer (or, more correctly, whichever computer we assign to the variable strComputer):<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>Our next step is to retrieve the path to the All Users Desktop folder. To do that we make a second WMI connection, this time to the System Registry provider. (Could we have written this script using just one WMI connection? Sure, but it seemed a little easier to follow if we made one connection for retrieving the list of files and a second connection for reading information from the registry). After making the second connection we then assign values to a pair of variables:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strKeyPath<\/B> is assigned the path to the appropriate registry key. In this case, that\u2019s Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strValueName<\/B> is assigned the name of the actual registry value we want to read (Common Desktop).<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That brings us to this line of code:<\/P><PRE class=\"codeSample\">objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strAllUsers\n<\/PRE>\n<P>Here we\u2019re using the <B>GetStringValue<\/B> method to read the Common Desktop registry value. We pass GetStringValue four parameters: the constant HKEY_LOCAL_MACHINE; the variables strKeyPath and strValueName; and an \u201cout\u201d parameter named strAllUsers. Notice that we\u2019ve never assigned a value to strAllUsers. That\u2019s because we don\u2019t have to: with an out parameter the method will assign a value to the variable. And, in this case, that value will be the path to the All Users Desktop folder.<\/P>\n<P>Once we have the path to the All Users Desktop folder we can then use the following WMI query to retrieve a collection of all the files found in that folder:<\/P><PRE class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name='&#8221; &amp; strAllUsers &amp; &#8220;&#8216;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>And once we have that collection we can set up a For Each loop to walk through the list of files, echoing back the local path for each one:<\/P><PRE class=\"codeSample\">For Each objFile In colFileList\n    Wscript.Echo objFile.Name\nNext\n<\/PRE>\n<P>The net result looks something like this:<\/P>\n<P>c:\\documents and settings\\all users\\desktop\\desktop.ini<BR>c:\\documents and settings\\all users\\desktop\\security configuration wizard.lnk<\/P>\n<P>All that\u2019s left now is to repeat the process, this time getting the path to and echoing the files in the current user\u2019s Desktop folder. After that, we\u2019re done.<\/P>\n<P>Which means, of course, that we were right all along: this <I>was<\/I> an easy question to answer. Or at least it was once we finally figured out how to answer it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I list all the files on a user\u2019s desktop?&#8212; BT Hey, BT. So you thought you could fool the Scripting Guys, did you? Well, you did, at least for awhile. As is so often the case with the Scripting Guys we took one look at your question and thought, \u201cLet\u2019s [&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,38,11,3,707,94,12,5],"class_list":["post-67573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-files","tag-folders","tag-scripting-guy","tag-shell-application","tag-special-folders","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I list all the files on a user\u2019s desktop?&#8212; BT Hey, BT. So you thought you could fool the Scripting Guys, did you? Well, you did, at least for awhile. As is so often the case with the Scripting Guys we took one look at your question and thought, \u201cLet\u2019s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67573","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=67573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67573\/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=67573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}