{"id":70473,"date":"2005-02-09T08:57:00","date_gmt":"2005-02-09T08:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/09\/how-can-i-change-the-target-of-a-desktop-shortcut\/"},"modified":"2005-02-09T08:57:00","modified_gmt":"2005-02-09T08:57:00","slug":"how-can-i-change-the-target-of-a-desktop-shortcut","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-target-of-a-desktop-shortcut\/","title":{"rendered":"How Can I Change the Target of a Desktop 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 change the target that a desktop shortcut points to? For example, if I move a file from one server to another, I\u2019d like be able to use a script to change the shortcut that points to that file.<BR><BR>&#8212; AK<\/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, AK. This is an example of a scripting problem that\u2019s actually pretty easy to solve, provided you know where to look for the solution. If you\u2019re like most scripters, your first thoughts would be to use WMI or WSH; when both of those technologies turn out to be dead-ends you might think that maybe you can\u2019t change shortcut targets using a script after all. Ah, but just when all seemed lost, who should come swooping in to save the day but our oft-neglected friend, the Shell object.<\/P>\n<P>The Shell object is a kind of ragtag collection of scripting objects, many of which are either of minimal use to system administrators or whose functions are performed better\/faster\/easier using another scripting technology. But every now and then the Shell object provides an answer where none of these other scripting technologies are of much help. Modifying shortcut properties turns out to be just such a case.<\/P>\n<P>We\u2019re going to assume that you already know the location of the desktop shortcut that needs to be changed; for this example we\u2019ll use a hypothetical shortcut named <I>Accounts Payable Database.lnk<\/I> found in the All Users desktop folder. With that in mind, here\u2019s a script that changes the target of that shortcut to <B>\\\\atl-fs-01\\accounting\\payable.exe<\/B>:<\/P><PRE class=\"codeSample\">Const ALL_USERS_DESKTOP = &amp;H19&amp;<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(ALL_USERS_DESKTOP)\nSet objFolderItem = objFolder.ParseName(&#8220;Accounts Payable Database.lnk&#8221;)\nSet objShellLink = objFolderItem.GetLink<\/p>\n<p>objShellLink.Path = &#8220;\\\\atl-fs-01\\accounting\\payable.exe&#8221;\nobjShellLink.Save()\n<\/PRE>\n<P>We start out by creating a constant ALL_USERS_DESKTOP and setting the value to &amp;H19&amp;. We then create an instance of the Shell object, and use the <B>Namespace<\/B> method to bind to the desktop folder. We use the <B>ParseName<\/B> method to link to the file itself (note that we have the specify only the file name &#8211; <I>Accounts Payable Database.lnk <\/I>&#8211; and not the entire path), and then use the <B>GetLink<\/B> method to retrieve the shortcut information.<\/P>\n<P>After that it\u2019s easy. We set the value of the <B>Path<\/B> property to the new shortcut target, and then call the <B>Save()<\/B> method to write this value to the shortcut itself. Voila: we\u2019ve managed to change the target that this shortcut points to.<\/P>\n<P>One thing to keep in mind here is that the Shell object is designed to work only on the local computer; you can\u2019t create an instance of the Shell object on a remote machine. If you need to modify a shortcut on a remote computer, you\u2019ll either need to run this as part of a logon script, or use a process similar to the one described in the September 1, 2004 <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0901.mspx\"><B><I>Hey, Scripting Guy! <\/I><\/B><\/A>column to first copy the script to the remote computer and then start the copied script on that machine.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change the target that a desktop shortcut points to? For example, if I move a file from one server to another, I\u2019d like be able to use a script to change the shortcut that points to that file.&#8212; AK Hey, AK. This is an example of a scripting problem [&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,3,94,5],"class_list":["post-70473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-scripting-guy","tag-special-folders","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I change the target that a desktop shortcut points to? For example, if I move a file from one server to another, I\u2019d like be able to use a script to change the shortcut that points to that file.&#8212; AK Hey, AK. This is an example of a scripting problem [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70473","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=70473"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70473\/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=70473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}