Home > Back-end >  How do I get the target of a web shortcut lnk file?
How do I get the target of a web shortcut lnk file?

Time:05-06

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!

CodePudding user response:

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.

CodePudding user response:

Jeff Zeitlin, thank you, that ended up being the answer for me! I ended up having to do a little more looking to create a custom icon for a URL (uses a slightly different method). Looks like I'm going to have to remove the link and replace it with a URL. I'm using the following script (part of which I pilfered from a different stack overflow question):

$WshShell = New-Object -comObject WScript.Shell
$targetPath = "http://internalwebsite/subsite"
$iconLocation = "https://internalwebsite/1.ico"
$iconFile = "IconFile="   $iconLocation
$path = "$publicDesktop\usefulname.url"
$Shortcut = $WshShell.CreateShortcut($path)
$Shortcut.TargetPath = $targetPath
$Shortcut.Save()

Add-Content $path "HotKey=0"
Add-Content $path "$iconfile"
Add-Content $path "IconIndex=0"

THEN, if I want to figure out information about the file, I can use:

get-content $path

This makes any future scripting against the path significantly easier. Thank you very much for the help!

  • Related