Use PowerShell to Find an Advertised Shortcut Target

Have you ever looked at the target of an .lnk shortcut and it appears to be greyed out/disabled? Chances are it is a Windows Installer advertised shortcut, which is used as part of Windows Installer resiliency and self healing. Instead of it typically pointing to an executable, it invokes the Windows Installer subsystem to locate the shortcut target instead.

You can use PowerShell to find an advertised shortcut target like so:

function Get-AdvertisedShortcut {

    param([string]$pathToLnk)

    $shortcutTarget = ""
    
    if ($pathToLnk -ne $null -and (test-path $pathToLnk)) {    
        $windowsInstaller = New-Object -ComObject WindowsInstaller.Installer
        $lnkTarget = $WindowsInstaller.GetType().InvokeMember("ShortcutTarget","GetProperty",$null,$windowsInstaller,$pathToLnk)
        $productCode = $lnkTarget.GetType().InvokeMember("StringData","GetProperty",$null,$lnkTarget,1)
        $componentCode = $lnkTarget.GetType().InvokeMember("StringData","GetProperty",$null,$lnkTarget,3)
        $shortcutTarget = $WindowsInstaller.GetType().InvokeMember("ComponentPath","GetProperty",$null,$WindowsInstaller,@($productCode,$componentCode))        
    }

    return $shortcutTarget
}

$pathToLnk = Get-AdvertisedShortcut "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\InstEd.lnk"

Create Lnk Shortcut using VBScript

Dim objShell : Set objShell = CreateObject("WScript.Shell")
Dim userProfileFolder : userProfileFolder = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
Dim desktopFolder : desktopFolder = userProfileFolder & "\Desktop"
Dim programFilesFolder : programFilesFolder = objShell.ExpandEnvironmentStrings("%ProgramFiles%")

Dim shortcutName : shortcutName = "Alkane Test"
Dim shortcutDescription : shortcutDescription = "Alkane Solutions Description"
Dim shortcutArguments : shortcutArguments = ""
Dim workingDir : workingDir = programFilesFolder & "\AlkaneSolutions\"
Dim shortcutTarget : shortcutTarget = workingDir & "test.exe"
Dim shortcutIcon : shortcutIcon = workingDir & "test.exe, 2"

'Create the shortcut
Dim lnk : Set lnk = objShell.CreateShortcut(desktopFolder & "\" & shortcutName & ".lnk")
lnk.TargetPath = shortcutTarget
lnk.Arguments = shortcutArguments
lnk.Description = shortcutDescription
lnk.HotKey = "ALT+CTRL+F"
lnk.IconLocation = shortcutIcon
lnk.WindowStyle = "1"
lnk.WorkingDirectory = workingDir
lnk.Save
   
'Clean up 
Set lnk = Nothing
Set objShell = Nothing