How Can I Change the Icon for an Existing Shortcut?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the icon for an existing shortcut?

— DF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DF. You know, the Scripting Guys (some of them, at least) are old enough to remember a time when you would never even think about changing an icon; after all, back in those days having any icon at all was considered cool. (And if you had flying toasters on top of that, well ….) But times have changed, and apparently people aren’t satisfied with their icons anymore: yours is the fourth or fifth email we’ve gotten in the past few weeks asking if we could demonstrate a way to change icons using a script. And you know our motto here at Scripting Guys Central: the customer is always right.

Hey, we said that was our motto, we didn’t say we actually followed it.

Here’s the script:

Const DESKTOP = &H10&

Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.NameSpace(DESKTOP)

Set objFolderItem = objFolder.ParseName(“Test Shortcut.lnk”) Set objShortcut = objFolderItem.GetLink

objShortcut.SetIconLocation “C:\Windows\System32\SHELL32.dll”, 13 objShortcut.Save

Before getting into the details we should mention the fact that this particular script must be run locally; that’s because it uses the Shell object, and the Shell object cannot be created on a remote machine. Run this on your own machine or run it as a logon script; just don’t expect to run it against a remote computer. It won’t work.

As for the script itself, we begin by defining a constant named DESKTOP and setting the value to &H10&; this happens to be the value used by the Shell object to bind to the Desktop folder. We create an instance of the Shell.Application object, and then use the NameSpace method to bind to the user’s desktop folder.

Note. This is the user’s desktop folder, and not the All Users’ Desktop folder. For information on binding to All Users’ Desktop or other special folders, see the special folder scripts in the Script Center Script Repository.

After binding to the folder we then call the ParseName method to bind to an individual file within that folder; as you can see here, we’re binding to a file with the filename Test Shortcut.lnk:

Set objFolderItem = objFolder.ParseName(“Test Shortcut.lnk”)

Finally we use the GetLink property to return the ShellLinkObject object that represents the file Test Shortcut.lnk. Once we do that we’re ready to change the icon, something we can do using two lines of code:

objShortcut.SetIconLocation “C:\Windows\System32\SHELL32.dll”, 13

In the first line we call the SetIconLocation method and pass it two parameters: the path to the file containing the icon (in this case, C:\Windows\System32\Shell32.dll) and the index number of the icon within that file. How in the world do we know what index number to use? Here’s one way to find out:

Find a shortcut somewhere, right-click that shortcut, and then click Properties.

On the Shortcut tab, click Change Icon.

In the Change Icon dialog box, click Browse and then locate the file containing your icon. If you chose C:\Windows\System32\Shell32.dll then your screen should look something like this:


Icons are numbered beginning with 0 in the upper left-hand corner and continuing with all the icons in column 1. Here’s a revised graphic in which we’ve numbered some of the icons for you:


After setting the icon location and specifying the icon index, you call the Save method and – just like that – your shortcut icon will be changed.

Now, if they had an icon of a flying toaster we could understand why you’d want to use that. Otherwise….


Discussion is closed.

Feedback usabilla icon