4

I am wanting to change the icon for an existing shortcut using Powershell.

I played around with it but I couldn't set the changes so I went online and came up empty-handed. I've seen examples with VB and Command Shell but nothing with Powershell. Nearly 99% demonstrated how to create the shortcut but I just need to know how to change the icon and only the icon.

Here is what I did so far:

# Make a copy of the icon in the directory
PS> cd Program Files (x86)\Dir\Fol\
PS Program Files (x86)\Dir\Fol> cp 1234567890.ico 12345678901.ico

# Gets the IconFile property and changes it to the new icon
PS Program Files (x86)\Dir\Fol> cd Desktop\Folder\
PS Desktop\Folder>(Get-Content "file name")[6]
IconFile=C:\Program Files (x86)\Dir\Fol\1234567890.ico
PS Desktop\Folder>(Get-Content "file name")[6] -replace ".ico","1.ico"

I've tried working with the WScript.Shell ComObject but that seems to only create new shortcuts.

I feel like where I was going with it would work if there was a way to save, update, and apply the new IconFile path in the object.

I'm doing this as a fix action due to icons (or maybe something in the symbolic LNK?) "breaking" and defaulting to a generic icon. Seems to only be an issue with shortcuts. The shortcut works perfectly, everything is fine but for one reason or another, the icons default. If I go in and reapply the same icon name through the GUI, it won't change. However, if I change the name of the .ico file by any measure and then set it, it works. Don't know why it is doing it but I was wanting to create a PS script that would do it automatically (and I was looking for an excuse to jam out a script)

Rincewind
  • 412
  • 1
  • 10
  • 26
  • It's not a lnk-shortcut but just a plain text file, see [the unofficial format reference](http://www.lyberty.com/encyc/articles/tech/dot_url_format_-_an_unofficial_guide.html). – wOxxOm Jan 17 '17 at 21:39

3 Answers3

6

The wscript.shell CreateShortcut method will create a new OR open an existing shortcut. Here's a short script, where you will need to define $ShortcutPath, $IconLocation, and $IconArrayIndex:

$Shell = New-Object -ComObject ("WScript.Shell")
$Shortcut = $Shell.CreateShortcut($ShortcutPath)
$Shortcut.IconLocation = "$IconLocation, $IconArrayIndex"
$Shortcut.Save()
BenH
  • 9,766
  • 1
  • 22
  • 35
  • I saw the `$Shell.CreateShortcut()` before but I thought that was going to create a new shortcut. I'm guessing it creates an object of that shortcut so that it can be accessed programmatically? What exactly is the `$IconArrayIndex`? I think I remember seeing it was listed as 0. – Rincewind Jan 17 '17 at 21:52
  • @wOxxOm Would you mind putting that as an answer? And mention to `$shortcut.Save()` when complete? I'll select it as my answer--it worked after fiddling a bit. – Rincewind Jan 17 '17 at 22:18
  • 1
    There isn't need for the trickery. If the shortcut already exists, it opens it. [Quoted from MSDN](https://msdn.microsoft.com/en-us/library/xsy6k3ys(v=vs.84).aspx): _CreateShortcut Method Creates a new shortcut, or opens an existing shortcut._ Also tested working on Win10. – BenH Jan 17 '17 at 22:27
  • 1
    If there is only one image in an icon array, the index will be 0. Frequently there are many icons in the array for example `%SystemRoot%\System32\pmcsnap.dll` has 15. – BenH Jan 17 '17 at 22:30
6

for me .iconlocation does not exist in $shortcut and gives an error (might be the powershell version...). but here is how changed the url icon to a custom one i made: (the main part is the add-content commands)

$WshShell = New-Object -comObject WScript.Shell

$path = "C:\Users\USER\desktop\WEBSITE.url"

$targetpath = "https://WEBSITE.com"

$iconlocation = "C:\Users\USER\Desktop\YourIcon.ico"

$iconfile = "IconFile=" + $iconlocation

$Shortcut = $WshShell.CreateShortcut($path)

$Shortcut.TargetPath = $targetpath

$Shortcut.Save()

Add-Content $path "HotKey=0"

Add-Content $path "$iconfile"

Add-Content $path "IconIndex=0"
McCygnus
  • 4,187
  • 6
  • 34
  • 30
user8127749
  • 61
  • 1
  • 1
3

I was writing a script to change the default folder icon to the icon of a Network drive. This might help somewhere. The command is:

$ShortCut.IconLocation = "C:\WINDOWS\system32\imageres.dll, 28";