{"id":65283,"date":"2007-03-20T01:47:00","date_gmt":"2007-03-20T01:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/20\/how-can-i-delete-a-desktop-shortcut-if-i-dont-know-the-shortcuts-file-path\/"},"modified":"2007-03-20T01:47:00","modified_gmt":"2007-03-20T01:47:00","slug":"how-can-i-delete-a-desktop-shortcut-if-i-dont-know-the-shortcuts-file-path","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-a-desktop-shortcut-if-i-dont-know-the-shortcuts-file-path\/","title":{"rendered":"How Can I Delete a Desktop Shortcut If I Don\u2019t Know the Shortcut\u2019s File Path?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! I need to delete some desktop shortcuts; however, I don\u2019t know which folder those shortcuts might be in. For example, they might be in C:\\Documents and Settings\\All Users\\Desktop or they might be in, say, C:\\Documents and Settings\\kenmyer\\Desktop. How can I delete these shortcuts if I\u2019m not sure where they\u2019re located?<BR><BR>&#8212; SB<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, SB. You know, this is kind of a bad time; the second half of the UNLV-Georgia Tech game is just about to start. It\u2019s hard enough to try and watch a basketball game at work as it is; to then have to answer a scripting question at the same time is darn near impossible.<\/P>\n<P>On the other hand, Virginia is currently destroying Albany (66-45 with less than 8 minutes remaining), so maybe we <I>can<\/I> take some time out to answer this question after all. And before you ask, yes, the Scripting Guy who writes this column <I>did<\/I> pick Albany to upset Virginia, a selection that caused the Scripting Son to take one look at his father\u2019s choices and go, \u201cLook, if you\u2019re not even going to try then why bother making picks in the first place?\u201d <\/P>\n<P>Strong words from the Scripting Son, especially in light of the fact that, after day 1 of the NCAA men\u2019s basketball tournament, the Scripting Dad had the lead over the Scripting Son and the Scripting Brother in their annual March Madness challenge. In fact, if Gonzaga could have made a few shots and if Albany could have \u2013 well, OK, made a <I>lot<\/I> of shots \u2013 the Scripting Guy who writes this column would have a perfect score at the moment.<\/P>\n<P>But then again, that\u2019s the story of his life: he\u2019s always <I>this<\/I> close to being perfect. <\/P>\n<P>Do you realize that, as this column is being written, North Texas only trails Memphis by 6? Unbelievable.<\/P>\n<P>Anyway, on the off-chance that some of you aren\u2019t obsessed with the ups and downs of college basketball, here\u2019s a script that can delete a desktop shortcut regardless of the folder where that shortcut actually resides:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_ShortcutFile Where FileName = &#8216;Digital Voice Editor 2&#8242;&#8221;)<\/p>\n<p>For Each objItem in colItems\n    If Instr(objItem.Name, &#8220;desktop&#8221;) Then\n        strPath = objItem.Name\n        strPath = Replace(strPath, &#8220;\\&#8221;, &#8220;\\\\&#8221;)\n        Set colFiles = objWMIService.ExecQuery _\n            (&#8220;Select * From CIM_Datafile Where Name = &#8216;&#8221; &amp; strpath &amp; &#8220;&#8216;&#8221;)\n        For Each objFile in colFiles\n            objFile.Delete\n        Next\n    End If\nNext\n<\/PRE>\n<P>Can we explain how this works? Let\u2019s see \u2026 well, Virginia just called timeout, so the answer to that is yes, we can explain how this works. (Though we\u2019ll have to do it before the 30-second timeout is over.) What we\u2019ve got here is a desktop shortcut named <I>Digital Voice <\/I><I>Ed<\/I><I>itor 2<\/I>. We want to delete it, but we don\u2019t know for sure which folder the shortcut resides in. Is that a problem? Sure sounds like one, doesn\u2019t it?<\/P>\n<P>As it turns out, though, this is no problem at all. To delete the shortcut we start out by connecting to the WMI service on the local computer. (Although you can easily modify this script to remove desktop shortcuts from a remote computer: all you have to do is assign the name of that computer to the variable strComputer). After making the connection we then use the <B>ExecQuery<\/B> method and the <B>Win32_ShortcutFile<\/B> class to return a collection of all the shortcut files that have a <B>FileName<\/B> of <I>Digital Voice <\/I><I>Ed<\/I><I>itor 2<\/I>:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_ShortcutFile Where FileName = &#8216;Digital Voice Editor 2&#8242;&#8221;)\n<\/PRE>\n<P>Just a second, SB: Memphis is now up by 13. That\u2019s more what we expected. On the other hand, Albany is now trailing 80-55. In retrospect, picking Albany to win might have been a mistake.<\/P>\n<P>But hey, there\u2019s still 55 seconds left.<\/P>\n<P>Now, back to work. As it always does, the ExecQuery method returns a collection of items; in this case, that\u2019s a collection of all the shortcuts that have the name <I>Digital Voice <\/I><I>Ed<\/I><I>itor 2<\/I>. That means that our next step is to set up a For Each loop that loops through that entire collection:<\/P><PRE class=\"codeSample\">For Each objItem in colItems\n<\/PRE>\n<P>The first thing we do inside that loop is actually an optional step. Because SB specifically mentioned desktop shortcuts we use the <B>InStr<\/B> function to determine whether the term <I>desktop<\/I> appears anywhere within the file <B>Name<\/B> (which, when it comes to the Win32_ShortcutFile class, is equivalent to the file path):<\/P><PRE class=\"codeSample\">If Instr(objItem.Name, &#8220;desktop&#8221;) Then\n<\/PRE>\n<P>This enables us to remove <I>desktop<\/I> shortcuts while retaining, say, Start Menu shortcuts. If you\u2019d prefer to delete <I>all<\/I> the shortcuts to a file (whether or not they happen to be on the desktop) then simply delete the If-Then statement. <\/P>\n<P>And yes, we know: every time UNLV pulls ahead Georgia Tech comes roaring back. Of course, we can\u2019t actually <I>watch<\/I> that game because CBS insists that either UNLV or Georgia Tech is in the same geographical area as Redmond, Washington. Apparently we don\u2019t know as much about geography as we thought we did.<\/P>\n<TABLE class=\"dataTable\" id=\"EVF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Geographic Update<\/B>. The Notre Dame (Indiana) and Winthrop (North Carolina) game is blacked-out for the same geographical reason. OK \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, where were we? Oh, right: inside the For Each loop. As you can see, inside the loop we assign the value of the file <B>Name<\/B> (which, again, is the same thing as the file path) to a variable named strPath. And then we encounter this line of code:<\/P><PRE class=\"codeSample\">strPath = Replace(strPath, &#8220;\\&#8221;, &#8220;\\\\&#8221;)\n<\/PRE>\n<P>Why do we need this line of code? Well, our file path is going to be something like this: C:\\Documents and Settings\\All Users\\Desktop<I>\\<\/I>Digital Voice Editor 2.lnk. There\u2019s nothing wrong with that; that\u2019s what a file path is <I>supposed<\/I> to look like. However, to actually delete this shortcut we need to use that path in a WMI query. That\u2019s a problem, because the \\ is a reserved character and can\u2019t be used in a query without first being \u201cescaped\u201d (which simply means that you need to preface each of those characters with another \\). <\/P>\n<P>And <I>that\u2019s<\/I> what we use the <B>Replace<\/B> function for: we replace each \\ with a \\\\. That gives us a path (stored in the variable strPath) that looks like this: C:\\\\Documents and Settings\\\\All Users\\\\Desktop<I>\\\\<\/I>Digital Voice Editor 2.lnk.<\/P>\n<P>That also gives us a path we can use in a WQL query:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_Datafile Where Name = &#8216;&#8221; &amp; strpath &amp; &#8220;&#8216;&#8221;)\n<\/PRE>\n<P>What\u2019s this query going to do? It\u2019s going to return a collection of all the files that have a Name (path) equal to the value of the variable strPath. And after it does so we\u2019re going to use this block of code to loop through that collection and delete the corresponding shortcut file:<\/P><PRE class=\"codeSample\">For Each objFile in colFiles\n    objFile.Delete\nNext\n<\/PRE>\n<P>And then we swing around and repeat the process, just in case there are any other shortcuts named <I>Digital Voice <\/I><I>Ed<\/I><I>itor 2<\/I> that we need to delete.<\/P>\n<P>We hope that helps, SB. As for the Scripting Guy who writes this column, nothing will help; he\u2019s having a miserable day. At lunch time they had a projector TV set up in the cafeteria. NCAA basketball, right? Wrong: instead, they were showing a <I>cricket match<\/I>. (Yes, a cricket match.) Not long after lunch the Scripting Guy who writes this column had to go to a meeting, meaning that he missed both Wisconsin\u2019s frenzied comeback against Texas A&amp;M-Corpus-Christi <I>and<\/I> Nevada\u2019s overtime win over Creighton. Between cricket matches, meetings, and the need to do his job the Scripting Guy who writes this column has hardly had a chance to watch <I>any<\/I> basketball whatsoever. Apparently Microsoft <I>has<\/I> lost its sense of priorities.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to delete some desktop shortcuts; however, I don\u2019t know which folder those shortcuts might be in. For example, they might be in C:\\Documents and Settings\\All Users\\Desktop or they might be in, say, C:\\Documents and Settings\\kenmyer\\Desktop. How can I delete these shortcuts if I\u2019m not sure where they\u2019re located?&#8212; SB Hey, [&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,47,3,5],"class_list":["post-65283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to delete some desktop shortcuts; however, I don\u2019t know which folder those shortcuts might be in. For example, they might be in C:\\Documents and Settings\\All Users\\Desktop or they might be in, say, C:\\Documents and Settings\\kenmyer\\Desktop. How can I delete these shortcuts if I\u2019m not sure where they\u2019re located?&#8212; SB Hey, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65283","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=65283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65283\/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=65283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}