Skip to content

Instantly share code, notes, and snippets.

@SAPIENTechnologies
Last active December 14, 2016 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SAPIENTechnologies/f3a23bd640b2a0b80db0af9c9e4ba0a8 to your computer and use it in GitHub Desktop.
Save SAPIENTechnologies/f3a23bd640b2a0b80db0af9c9e4ba0a8 to your computer and use it in GitHub Desktop.
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