Link to home
Start Free TrialLog in
Avatar of IT Guy
IT GuyFlag for United States of America

asked on

Determine exactly what program file is being launched by examining program shortcut

How can I determine exactly what program file is being launched by examining the program's shortcut within Windows 10?

The exact path and filename of the program that is being launched aren't always intuitively obvious by examing the properties of the shortcut.

For example, the "Start in" section of the Adobe Acrobat XI Pro application is completely blank (see the screenshot).

So what is the best method to use to determine exactly what path and application is being launched when the program shortcut is clicked on?

User generated image
Avatar of John
John
Flag of Canada image

For Adobe, Properties, Shortcut and see "C:\Program Files (x86)\Adobe\Acrobat 2017\Acrobat\Acrobat.exe"

For Word 2016, same process, see "C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE"

If the shortcut to open is a .LNK file, then it is probably not a good idea to open (at least I would not)

https://www.howtogeek.com/190897/how-can-you-open-and-edit-windows-.lnk-shortcut-files/
Avatar of Qlemo
"Start in" can be empty, it is only used as the default path the program will use for searching files. Programs like Acrobat often have their own default path definitions, so don't need the "Start in" setting.

The important information is, as shown by John, the complete path to the application file stored in the shortcut in the same field as the executable name. There are some exceptions where the executable name is not stored in the shortcut - instead, it is a link to an installer file managing the application.
right click the shortcut on the start menu

hover on More (near top) and select Open File Location

you will now see the folder with the REAL shortcut - right click on this shortcut and select Properties

here is where VLC media player lives on MY PC:
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"
The exact path and filename of the program that is being launched aren't always intuitively obvious by examing the properties of the shortcut.

that's because the application installer (at least with adobe cc apps and microsoft office) create a CLSID shortcut
in the lnk file, it has the target as the CLSID as the application is described in the registry hence some of the buttons and text boxes are not available to be modified

well documented when searching for 'windows shortcut clsid'

For Adobe, Properties, Shortcut and see "C:\Program Files (x86)\Adobe\Acrobat 2017\Acrobat\Acrobat.exe"

that will vary on the version.  for me, acrobat is in C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Acrobat.exe
same with office; can also vary by architecture.  mine is installed in C:\Program Files\Microsoft Office\root\Office16
Avatar of oBdA
oBdA

These are called "advertised shortcuts"; you can drag the information out using PowerShell.
For advertised shortcuts, the property AdvertisedTargetPath will have the real target path.
Save it as Get-Shortcut.ps1 or Whatever.ps1; it supports pipeline input, so you can do something like that to find all advertised shortcuts:
gci "C:\ProgramData\Microsoft\Windows\Start Menu\*.lnk" -Recurse | .\Get-Shortcut.ps1 | ? {$_.AdvertisedTargetPath}

Open in new window

Or to see all the results in a GUI based table, where you can sort and filter, pipe the results further to Out-GridView:
gci "C:\ProgramData\Microsoft\Windows\Start Menu\*.lnk" -Recurse | .\Get-Shortcut.ps1 | Out-GridView

Open in new window

[CmdletBinding()]
Param(
	[Parameter(ValueFromPipeline=$true)]
	[string[]]$Path
)
Begin {
	$shell = New-Object -ComObject WScript.Shell
	$installer = New-Object -ComObject WindowsInstaller.Installer
}
Process {
	$Path | ForEach-Object {
		If ($item = Get-Item -Path $_ -ErrorAction SilentlyContinue) {
			If ($item.Extension -eq '.lnk') {
				$shortcut = $shell.CreateShortcut($_) | Select-Object -Property @{n='Name'; e={$item.BaseName}}, *, AdvertisedTargetPath
				If ($shortcut.TargetPath.ToLower().StartsWith("${env:Systemroot}\Installer\".ToLower())) {
					$shortcutTarget = $installer.GetType().InvokeMember('ShortcutTarget', 'GetProperty', $null, $installer, $item.FullName)
					$productCode = $shortcutTarget.GetType().InvokeMember('StringData', 'GetProperty', $null, $shortcutTarget, 1)
					$componentCode = $shortcutTarget.GetType().InvokeMember('StringData', 'GetProperty', $null, $shortcutTarget, 3)
					$shortcut.AdvertisedTargetPath = $installer.GetType().InvokeMember('ComponentPath', 'GetProperty', $null, $installer, @($productCode, $componentCode))
				}
				$shortcut
			} Else {
				Write-Error "Item '$($_)' is not a shortcut!"
			}
		} Else {
			Write-Error "Path '$($_)' not found!"
		}
	}
}
End {
	[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($shell)
	[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($installer)
}

Open in new window

Avatar of IT Guy

ASKER

oBda,

When I try to run this PowerShell command from within my Windows 10 PowerShell (which is running as an administrator) I simply get a blinking cursor and no output or messages are returned. I have verified that the excel.lnk file exists within the specified path and in fact, have copied this exact path into this PowerShell command.

Please let me know what I need to do to fix this so I will be able to run this PowerShell command. Do I need to load any special PowerShell consoles or do I need to run any other PowerShell commands before running the one(s) that you have provided?

gci "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\excel.lnk" -Recurse | .\Get-Shortcut.ps1 | Out-GridView

Open in new window


 User generated image
Avatar of IT Guy

ASKER

Also what filename should I save this PowerShell file as?

I have given it different names such as "Get-Shortcut.ps1" or "1.ps1" and it still doesn't work.
If this is the first PowerShell script you ever run, open a PS console and enter
Set-ExecutionPolicy RemoteSigned -Force

Open in new window

Then for starters, save it as Get-Shortcut.ps1 in C:\Temp
Then run (Out-Gridview is not very useful for a single shortcut):
gci "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\*.lnk" -Recurse | ? {$_.BaseName -like "*Excel*"} | C:\Temp\Get-ShortcutNew.ps1

Open in new window

Avatar of IT Guy

ASKER

oBda,

I have done exactly what you have said and here are the results:

PS C:\temp> .\Get-Shortcut.ps1
C:\Temp\Get-ShortcutNew.ps1 : The term 'C:\Temp\Get-ShortcutNew.ps1' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At C:\temp\Get-Shortcut.ps1:1 char:111
+ ... curse | ? {$_.BaseName -like "*Excel*"} | C:\Temp\Get-ShortcutNew.ps1
+                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Temp\Get-ShortcutNew.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\temp> Get-Shortcut.ps1
Get-Shortcut.ps1 : The term 'Get-Shortcut.ps1' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Get-Shortcut.ps1
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-Shortcut.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


Suggestion [3,General]: The command Get-Shortcut.ps1 was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: ".\Get-Shortcut.ps1". See "get-help about_Command_Precedence" for more details.
PS C:\temp>

User generated image
That was a copy and paste error; since the file is called Get-Shortcut.ps1, the last command must obviously be C:\Temp\Get-Shortcut.ps1, not C:\Temp\Get-ShortcutNew.ps1.
gci "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\*.lnk" -Recurse | ? {$_.BaseName -like "*Excel*"} | C:\Temp\Get-Shortcut.ps1

Open in new window

Avatar of IT Guy

ASKER

oBda,

I'm sorry I still don't understand what you mean.

Can you please repost everything again from start to finish including these PowerShell scripts, their names, their folder locations, and what they are supposed to do?

Thank you for your help and patience I am still learning the PowerShell.
Save the script as C:\Temp\Get-Shortcut.ps1
In a PowerShell console, enter
Set-ExecutionPolicy RemoteSigned -Force
gci "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\*.lnk" -Recurse | ? {$_.BaseName -like "*Excel*"} | C:\Temp\Get-Shortcut.ps1

Open in new window

Avatar of IT Guy

ASKER

Once again when I run this script (on either one of my Windows 10 computers) the script just hangs and never responds.

What needs to be done to fix this?

User generated image
This happens both when I run the script exactly like it is posted above or if I edit it like this:

Set-ExecutionPolicy RemoteSigned -Force
gci "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\excel.lnk" -Recurse | ? {$_.BaseName -like "*Excel*"} | C:\Temp\Get-Shortcut.ps1

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT Guy

ASKER

oBda,

Great! This is exactly what I am looking for.

The one-line scripts work fine.

However, when I saved the longer script within the C:\Temp folder (as C:\Temp\Get-Shortcut.ps1) I get this error:

What needs to be done to fix this?

PS C:\temp> .\Get-Shortcut.ps1
Get-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\temp\Get-Shortcut.ps1:13 char:30
+         If ($item = Get-Item -Path $_ -ErrorAction SilentlyContinue)  ...
+                                    ~~
    + CategoryInfo          : InvalidData: (:) [Get-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemCommand

PS C:\temp>

User generated image
Pass it a valid path of a .lnk file as argument. It doesn't actively search for files, that's what Get-ChildItem (alias gci, alias dir) is for.
C:\temp\Get-Shortcut.ps1 -Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Calculator.lnk"

Open in new window

Avatar of IT Guy

ASKER

In which line of this PowerShell script do I provide the valid path of a .lnk file?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT Guy

ASKER

oBdA,

Thank you very much for your help with this.

This is greatly appreciated.