Search code examples
powershellshortcut

How do I get the target of a web shortcut lnk file?


This is a rather unique issue, I'm finding. I have created a web shortcut using the following script:

$target = "http://internalwebsite/subsite"
$shortcut_lnk = "$publicDesktop\usefulname.lnk"
$wscriptshell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($Shortcut_lnk)
$Shortcut.TargetPath = $Target
$ShortCut.IconLocation = "https://internalwebsite/1.ico, 0";
$Shortcut.Save()

Unfortunately, the target is http instead of HTTPS. I would like to detect any shortcuts that have the unsecure version and replace it with the secure version.

I've found the following code, which is supposed to give me the information I need:

$shortcut = 'C:\users\public\Desktop\usefulname.lnk'
$sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut($shortcut).TargetPath

However, this returns no data. If I just run:

$sh.CreateShortcut($shortcut)

It returns:

FullName         : C:\users\public\Desktop\usefulname.lnk
Arguments        : 
Description      : 
Hotkey           : 
IconLocation     : https://internalwebsite/1.ico,0
RelativePath     : 
TargetPath       : 
WindowStyle      : 1
WorkingDirectory : 

I've confirmed that the link works as intended. My question now - how do I get the actual web link? If I go to the properties of the link, I see that the target type is the website (http://internalwebsite/subsite) and the target is greyed out with the same info. But so far, my Google-fu has yielded nothing in regards to listing the target type, or explaining why I can't pull the path (or any alternative methods).

If anyone can help I would greatly appreciate it!


Solution

  • You need to create the web shortcut in a slightly different way. According to this TechNet forum posting, the filename must have an extension of .url rather than .lnk, and you must explicitly call the .Save() method of the object.