Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find an installation directory in the registry; more reliable than file system path search.
<#
.SYNOPSIS
Gets the installation directory for a program in the registry.
.DESCRIPTION
Get-InstallationPath gets the installation path for a program in the InstallProperties registry key. This method is much more reliable and change-resistant than searching the file system with partial paths.
.
The function returs a custom object with the DisplayName, Publisher, and InstallationPath so you can confirm that the path returns is for the intended program.
.
If the script cannot find the installation path for the program, it generates a non-terminating error.
.PARAMETER ProgramName
Specify the name of the program installed on the local system. Get-InstallationPath returns the installation directory of the specified program. Wildcard characters are supported.
.EXAMPLE
Get-InstallationPath -ProgramName "*PowerShell*Studio*"
DisplayName Publisher InstallPath
----------- --------- -----------
PowerShell Studio 2016 SAPIEN Technologies, Inc. C:\Program Files\SAPIEN Technologies, Inc\PowerShell Studio 2016\
.EXAMPLE
if (($result = Get-InstallationPath -ProgramName "*PowerShell*Studio*").Publisher -like "*SAPIEN*")
{ $path = $result.InstallPath }
PS C:\> $path
C:\Program Files\SAPIEN Technologies, Inc\PowerShell Studio 2016\
This command checks the publisher value before saving the installation directory in the $path variable.
.EXAMPLE
Get-InstallationPath -ProgramName NotThere
Get-InstallationPath : Cannot find installation directory for NotThere
At line:1 char:1
+ Get-InstallationPath -ProgramName NotThere
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-InstallationPath
.OUTPUTS
PS
.NOTES
Additional information about the function.
#>
function Get-InstallationPath
{
[CmdletBinding()]
[OutputType([PSCustomObject])]
Param
(
[Parameter(Mandatory = $true)]
[SupportsWildcards()]
[string]
$ProgramName
)
$result = @()
if ($inst = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\*\Products\*\InstallProperties" -ErrorAction SilentlyContinue)
{
$inst | Where-Object {
($DisplayName = $_.getValue('DisplayName')) -like $ProgramName
} |
ForEach-Object {
$result += [PSCustomObject]@{
'DisplayName' = $displayName
'Publisher' = $_.getValue('Publisher')
'InstallPath' = $_.getValue('InstallLocation')
}
}
}
else
{
Write-Error "Cannot get the InstallProperties registry keys."
}
if ($result)
{
return $result
}
else
{
Write-Error "Cannot get the InstallProperties registry key for $ProgramName"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.