How-to: Find the target of an Advertised Shortcut

Display the fully resolved target for an Advertised/MSI shortcut. Advertised shortcuts will automatically install the software on first run, so the destination file may not exist if the shortcut has never been used.

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\Excel.lnk"

via Alkane solutions

An older VBScript version

' GetRealTarget.vbs
' Run using cscript rather than wscript.

' Pass the full path to an MSI "Advertised Shortcut" file (including the extension) as a parameter.
' e.g. assuming a default All Users install of Office 2019:
' cscript GetRealTarget.vbs "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\excel.lnk" 
' 

Option Explicit
Dim MSITarget

On Error Resume Next
If wscript.arguments.count = 1 Then
   With CreateObject("WindowsInstaller.Installer")
      Set MSITarget = .ShortcutTarget(wscript.arguments(0))
      If Err = 0 then
         wscript.echo .ComponentPath(MSITarget.StringData(1), MSITarget.StringData(3))
      Else 
         wscript.echo wscript.arguments(0) & vbcrlf & "is not a legitimate MSI shortcut file or could not be found"
      End If
   End With
End If
On Error Goto 0

“The wrong advertising can actually reduce the sales of a product” ~ David Ogilvy


 
Copyright © 1999-2024 SS64.com
Some rights reserved