{"id":70853,"date":"2004-12-08T12:06:00","date_gmt":"2004-12-08T12:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/12\/08\/how-can-i-determine-the-executable-file-corresponding-to-a-shortcut\/"},"modified":"2004-12-08T12:06:00","modified_gmt":"2004-12-08T12:06:00","slug":"how-can-i-determine-the-executable-file-corresponding-to-a-shortcut","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-executable-file-corresponding-to-a-shortcut\/","title":{"rendered":"How Can I Determine the Executable File Corresponding to a Shortcut?"},"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 the executable file name corresponding to a .lnk shortcut file?<BR><BR>&#8212; AM<\/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, DE. We\u2019re glad you asked this question; unlike some questions we get (like, \u201cHow do I use a script to disable File and Print Sharing on my Windows 2000 computers?\u201d), this happens to be one we have an answer for.<\/P>\n<P>As it turns out, there\u2019s a WMI class &#8211; Win32_ShortcutFile &#8211; that can retrieve this information for you. All you have to do is query the Win32_ShortcutFile class and ask for the <B>Target<\/B> property; that\u2019s the name of the executable file (or script or document or whatever) that gets called when someone double-clicks the shortcut. Here\u2019s a simple little script that lists all the shortcut files on a computer, specifying the icon caption that you see in My Computer or Windows Explorer (<B>FileName<\/B>), the name of the corresponding executable file (<B>Target<\/B>), and the file path to the shortcut itself (<B>Description<\/B>):<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery(&#8220;Select * From Win32_ShortcutFile&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo &#8220;Name: &#8221; &amp; objFile.FileName\n    Wscript.Echo &#8220;Shortcut target: &#8221; &amp; objFile.Target\n    Wscript.Echo &#8220;File name: &#8221; &amp; objFile.Description\nNext\n<\/PRE>\n<P>Of course, you don\u2019t have to wade through all the shortcuts on a computer. Suppose you just want to locate the executable file for a single shortcut. In that case, you could use a script similar to this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_ShortcutFile WHERE FileName = &#8216;Adobe Photoshop Elements 2.0&#8242;&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo &#8220;Name: &#8221; &amp; objFile.FileName\n    Wscript.Echo &#8220;Shortcut target: &#8221; &amp; objFile.Target\n    Wscript.Echo &#8220;File name: &#8221; &amp; objFile.Description\nNext\n<\/PRE>\n<P>Here\u2019s another example, which checks to see if there are any shortcuts on a computer that reference Microsoft Virtual PC. Note that &#8211; per WMI conventions &#8211; any time you use a file path in a query you must use two \\\\\u2019s in path names rather than one:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_ShortcutFile WHERE Target = &#8221; &amp; _\n        &#8220;&#8216;C:\\\\Program Files\\\\Microsoft Virtual PC\\\\Virtual PC.exe'&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo &#8220;Name: &#8221; &amp; objFile.FileName\n    Wscript.Echo &#8220;Shortcut target: &#8221; &amp; objFile.Target\n    Wscript.Echo &#8220;File name: &#8221; &amp; objFile.Description\nNext\n<\/PRE>\n<P>That should give you a headstart on retrieving information about shortcuts. For those of you who\u2019ve asked about creating and deleting shortcuts, we\u2019ll address those topics very soon.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the executable file name corresponding to a .lnk shortcut file?&#8212; AM Hey, DE. We\u2019re glad you asked this question; unlike some questions we get (like, \u201cHow do I use a script to disable File and Print Sharing on my Windows 2000 computers?\u201d), this happens to be one we [&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-70853","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! How can I determine the executable file name corresponding to a .lnk shortcut file?&#8212; AM Hey, DE. We\u2019re glad you asked this question; unlike some questions we get (like, \u201cHow do I use a script to disable File and Print Sharing on my Windows 2000 computers?\u201d), this happens to be one we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70853","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=70853"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70853\/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=70853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}