Learn about Windows PowerShell
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
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"
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.
Hello, I am somewhat disoriented : your title announces that you want to modify the target of a shortcut, and then you explain how to open the explorer on that directory, directly from a script.
Just after I created a directory in a script, it seemed reasonable to plan two things :
- open the explorer to open the directory that was just created
- create a Windows shortcut that will do that again without the script.
Well well well ... How do you say where you want your shortcut ?
In fact, the problem I had, on Windows XP Home SP3, was that just after creating the shortcut, in its target, the argument "/e," was replaced by "\e," and of course that did not work, until I modified that by hand.
I found a turnaround : to avoid creating a new script (I use Powershell, that has a powerful syntax but takes 30 seconds to load), I created a batch, and a shortcut that calls the batch. I added a program to call the batch asynchronously.
This runs pretty good, but is someone able to find a solution, instead of a turnaround ?
So, if I created a $shortcut objet that contains a WshShell shortcut object, just after I execute
$shortcut.Target = "C:\Windows\Explorer.exe" /e, "C:\Documents and Settings"
and I ask to display $shortcut.Target, I obtain :
"C:\Windows\Explorer.exe" \e, "C:\Documents and Settings"
And unfortunately that is what I obtain as target of the script when I save it.
Well, how can I keep a slash rather than a backslash ?
This question is the same either you execute from Powershell of from VBScript.
Oh, I am sorry, I asked a question that was planned to be asked there :
But as the topic is to modify the target of a shortcut, maybe it is better to ask it here ?
Of course if it was the first intention I should have presented the target with the syntax "explorer /e, path" sooner in the message.
Why do I get this:
& : Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
+ CategoryInfo : ParserError: (:) , ParseException
+ FullyQualifiedErrorId : AmpersandNotAllowed
Why has my shortcut to facebook changed suddenly without any reason>????