The .IconLocation
property isn't supported in your case, but there's a workaround:
# ... icon download code omitted
$shortcutFile = Join-Path $wshShell.SpecialFolders.Item('AllUsersDesktop') 'myname.url'
$iconFile = 'C:\Users\Public\Pictures\filename.ico'
$wshShell = New-Object -ComObject "WScript.Shell"
$urlShortcut = $wshShell.CreateShortcut($shortcutFile)
$urlShortcut.TargetPath = 'https://en.wikipedia.org'
$urlShortcut.Save()
# This updates the .url file directly to emulate what assigning
# an icon interactively, via File Explorer, does.
@"
IconIndex=0
HotKey=0
IconFile=$iconFile
"@ | Add-Content -LiteralPath $shortcutFile
When you create a URL shortcut file (extension .url
):
Only one writable property is supported by the resulting WshUrlShortcut
object, namely TargetPath
, which stores the target URL.
Notably, this prevent use of the IconLocation
property, which is only available on executable shortcut files (extension .lnk
), which are WshShortcut
objects.
However, the .url
file format does support custom icons (by default, the default browser's icon is used), but that requires assigning them interactively, via File Explorer.
Fortunately, .url
files are plain-text, .ini
-like files, so it's easy to programmatically update that file directly, so as to emulate the results of interactively assigning an icon, as shown above.
Alternatively - which may or may not be an option in your case - you can create a regular shortcut file, with extension .lnk
, which - perhaps surprisingly - also works with URLs assigned to .TargetPath
. Assigning to .IconLocation
then works as usual.
However, there are ramifications:
Obviously, you'll end up with a different filename extension, and the shortcut file won't be readily recognizable as a URL shortcut by its extension.
.lnk
files are binary files.
.lnk
files with URLs as their target path inexplicably don't allow the URL to be edited via File Explorer later.