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.