How Can I Change the Icon for an Existing Shortcut?

How Can I Change the Icon for an Existing Shortcut?

  • Comments 6
  • Likes
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
objShortcut.Save

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


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:

Icons


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….

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This script did not work. error: Line 7, Char 1, Object required: 'objFolderItem' Code: 800A01A8

  • Same here, I tried it and it does not work. I really like the tutorial way you did this, but I wish you would follow up with answers.

  • Did Not Work for me neither

  • Hey, Scripting Guy! :P Tried this script it came back with "Ampersand not allowed. The & operator is reserved for future use; use "&" to pas
    s ampersand as a string.
    At line:2 char:17" it doesn't like &h10&

    Tired passing the ampersands as a string and the whole &h10& as a string but the script really shits the bed when i do that. I know this hasn't been replied to since last year but i figured maybe id give it a shot.

  • Looks like everything above is a Visual Basic script. Try instead:

    $objShell = New-Object -comObject Shell.Application
    $objDesktop = $objShell.NameSpace(0X0)
    $shortcutFilename = "My Shortcut Filename.lnk"
    $objFolderItem = $objDesktop.ParseName($shortcutFilename)
    $objShortcut = $objFolderItem.GetLink
    $objShortcut.SetIconLocation("c:\windows\system32\shell32.dll",27)
    $objShortcut.Save()

  • Works Fine, just make sure you define objShell, objFolder & objFolderItem as objects. Then you must create a new shortcut on your desktop. Simply do this by right clicking your mouse & selecting new shortcut, then use the browse button to link it to any file. Important to then name your shortcut 'Test Shortcut'... then run your script in VBA. you should find the shortcut icon changes right before your eyes. I spent ages looking for a solution to this, thankyou Script Guy.