0

I am trying to get the path to the executable from a shortcut, in Windows 7 with PowerShell 5.0, if that makes a difference. My $workingTarget = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\SnippingTool.lnk' and the shortcut is there. When I open properties on the shortcut the Target property of the shortcut shows %windir%\system32\SnippingTool.exe. However,

$shortcut = $shell.CreateShortcut($workingTarget)
Write-Host "$($shortcut.TargetPath)"

shows nothing at all. When I tried using a different shortcut, with a fully resolved path without an environment variable, that Write-Host is fine. My hope was to at least get the string and use Get-Command to find the path via the Paths environment variable, but even that is looking problematic since

Write-Host "$(Get-Command (Split-Path '%windir%\system32\SnippingTool.exe' -leaf))"
Write-Host "$(Get-Command (Split-Path 'SnippingTool.exe' -leaf))"

both just return SnippingTool.exe, not the correct full path. Now, on the bright side

Write-Host "$([System.Environment]::ExpandEnvironmentVariables('%windir%\system32\SnippingTool.exe'))"

does expand as expected, but only if I can actually get %windir%\system32\SnippingTool.exe out of the shortcut, which is vexing me at the moment.

Gordon
  • 6,257
  • 6
  • 36
  • 89
  • 1
    Some shortcuts are actually not normal shortcuts. They are [advertised shortcuts](https://www.symantec.com/connect/articles/about-windows-installer-shortcuts). They do not point to the executable. They point to a feature from an msi installation. – Olaf Sep 24 '18 at 00:25

1 Answers1

0

After a quick search on SO I found Jeffs answer which will satisfy your need.

Get target of shortcut (.lnk) file with powershell

$sh = New-Object -ComObject WScript.Shell $target = $sh.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Snipping Tool.lnk').TargetPath

Results when I run this in ISE:

[Admin] C:\WINDOWS\system32\> $sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Snipping Tool.lnk').TargetPath
$target
C:\WINDOWS\system32\SnippingTool.exe

Running

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      15063  1206
Drew
  • 3,814
  • 2
  • 9
  • 28
  • Did you test this and it is working for you? Because I tried it here and it is showing nothing. Which has me thinking either differences in PS version or something odd on my machine account for it. But I also tried it on two VMs, one Win7 and one Win10, with identical results. – Gordon Sep 24 '18 at 09:04
  • Interestingly enough, @Tim Lewis has a solution here (https://stackoverflow.com/questions/484560/editing-shortcut-lnk-properties-with-powershell/21967566#21967566) that DOES work. Much more code than either my solution or the ones being offered. So, off to see what the difference is. – Gordon Sep 24 '18 at 09:11
  • Hmm, even that doesn't work consistently. But, I will work around it since there seems not to be an answer. – Gordon Sep 24 '18 at 13:19