We help IT Professionals succeed at work.

Get fullname application via exepath, powershell

janhoedt
janhoedt used Ask the Experts™
on
Hi,

I have an executable path, f.e. C:\Program Files (x86)\XMind\XMind.exe.
How do I get the full name of the program via Powershell?

J
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NoahHardware Tester and Debugger

Commented:
Hi there! :)

Please try this.

get-childitem "C:\Program Files (x86)\XMind\XMind.exe" -recurse | where {$_.extension -eq ".exe"} | % {
     Write-Host $_.FullName
}

Open in new window

AlexA lack of information provides a lack of a decent solution.

Commented:
get-childitem "C:\Program Files (x86)\XMind\XMind.exe" | select-object Fullname

Not really any need for the recurse, the where either, Or the write host for that matter.
NoahHardware Tester and Debugger

Commented:
Alternatively, you can do this!

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize

Open in new window

AlexA lack of information provides a lack of a decent solution.

Commented:
A better question though, what is it you're actually trying to do, I know you've said you want the full name in powershell, but what are you actually trying to achieve?

Regards
Alex
NoahHardware Tester and Debugger

Commented:
@Alex Good point! Thanks. I did that because he did specify the file path leading to an .exe anyway.
AlexA lack of information provides a lack of a decent solution.

Commented:
Yeah but he's given you the "Leaf" object, you've already specified the entire path, your script would work fine don't get me wrong, but your script would be better utilised if you're pulling multiple objects from a higher level folder.

If you wanted to pull all EXE files from say, The Microsoft Office folder. Yes it'd work perfectly and if that's what he actually needs rather than what he's explained then your script is the much better option.

:-)

Author

Commented:
>what  do you want to achieve

"C:\Program Files (x86)\XMind\XMind.exe.
How do I get the full name of the program via Powershell?"

None of the solutions givee the full name. What works for another exe. f.e. Wors is get-item pathtoexe select versioninfo |select *

I want to map path of exe to full application name and version via Powershell
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
That's not done that easily, because the exe doesn't contain its product name, or the version embedded in the file may be different from the real product name. What's the use case anyway, if I may ask?
This tries its best to find it based on the uninstall information, but not all installations add information about the installation folder there (PowerShell 6/Core, 7-Zip for example), so it won't find those.
Save this as Get-ProductInfo.ps1 or Whatever.ps1.
Then just pass it the path to the exe:
.\Get-ProductInfo.ps1 -Command "C:\Program Files (x86)\XMind\XMind.exe"

Open in new window

[CmdletBinding()]
Param(
	[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
	[Alias('FullName', 'Path')]
	[String[]]$Command
)
Begin {
	$uninstall = ForEach ($hive in 'HKCU', 'HKLM') {
		ForEach ($architecture in '', '\Wow6432Node') {
			Get-ItemProperty "$($hive):\Software$($architecture)\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue |
			Where-Object {$_.DisplayName -and (-not $_.ParentKeyName) -and ($_.SystemComponent -ne 1)} |
			Select-Object -Property `
				@{n='ProductName'; e={$_.DisplayName}},
				@{n='ProductVersion'; e={$_.DisplayVersion}},
				@{n='Scope'; e={If ($hive -eq 'HKCU') {'User'} Else {'Machine'}}},
				@{n='Architecture'; e={If ($architecture) {'64bit'} Else {'32bit'}}},
				@{n='IconDir'; e={[IO.Path]::GetDirectoryName($_.DisplayIcon)}},
				@{n='InstallDir'; e={$_.InstallLocation.TrimEnd('\')}},
				@{n='UninstallDir'; e={If ($_.UninstallString -match '^"?(.+?\.exe)') {[IO.Path]::GetDirectoryName($Matches[1])}}}, PSChildName
		}
	}
}
Process {
	$Command | ForEach-Object {
		Try {
			$fullName = If ($_.Contains('\')) {(Resolve-Path -LiteralPath $_).Path} Else {(Get-Command -Name $_ -CommandType Application -ErrorAction Stop).Path}
			$commandDir = [IO.Path]::GetDirectoryName($fullName)
			$uninstall |
				Where-Object {($_.IconDir -and ($commandDir -like "$($_.IconDir)*")) -or ($_.InstallDir -and ($commandDir -like "$($_.InstallDir)*")) -or ($_.UninstallDir -and ($commandDir -like "$($_.UninstallDir)*"))} |
				Select-Object -Property ProductName, ProductVersion, Scope, Architecture, @{n='Command'; e={$fullName}}
		} Catch {
			$PSCmdlet.WriteError($_)
		}
	}
}

Open in new window