How Can I Change the Target of a Desktop Shortcut?

ScriptingGuy1

Hey, Scripting Guy! Question

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’d like be able to use a script to change the shortcut that points to that file.

— AK

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AK. This is an example of a scripting problem that’s actually pretty easy to solve, provided you know where to look for the solution. If you’re 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’t 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.

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.

We’re going to assume that you already know the location of the desktop shortcut that needs to be changed; for this example we’ll use a hypothetical shortcut named Accounts Payable Database.lnk found in the All Users desktop folder. With that in mind, here’s a script that changes the target of that shortcut to \\atl-fs-01\accounting\payable.exe:

Const ALL_USERS_DESKTOP = &H19&

Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.Namespace(ALL_USERS_DESKTOP) Set objFolderItem = objFolder.ParseName(“Accounts Payable Database.lnk”) Set objShellLink = objFolderItem.GetLink

objShellLink.Path = “\\atl-fs-01\accounting\payable.exe” objShellLink.Save()

We start out by creating a constant ALL_USERS_DESKTOP and setting the value to &H19&. We then create an instance of the Shell object, and use the Namespace method to bind to the desktop folder. We use the ParseName method to link to the file itself (note that we have the specify only the file name – Accounts Payable Database.lnk – and not the entire path), and then use the GetLink method to retrieve the shortcut information.

After that it’s easy. We set the value of the Path property to the new shortcut target, and then call the Save() method to write this value to the shortcut itself. Voila: we’ve managed to change the target that this shortcut points to.

One thing to keep in mind here is that the Shell object is designed to work only on the local computer; you can’t create an instance of the Shell object on a remote machine. If you need to modify a shortcut on a remote computer, you’ll 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 Hey, Scripting Guy! column to first copy the script to the remote computer and then start the copied script on that machine.

0 comments

Discussion is closed.

Feedback usabilla icon