Skip to content

Instantly share code, notes, and snippets.

@jmelosegui
Created January 10, 2019 16:53
Show Gist options
  • Save jmelosegui/58afaf6341007dfc2901da095e2faf64 to your computer and use it in GitHub Desktop.
Save jmelosegui/58afaf6341007dfc2901da095e2faf64 to your computer and use it in GitHub Desktop.
Retrieves a list of all software installed you can filter by name or guid
function Get-InstalledSoftware
{
<#
.SYNOPSIS
Retrieves a list of all software installed
.EXAMPLE
Get-InstalledSoftware
This example retrieves all software installed on the local computer
.PARAMETER Name
The software title you'd like to limit the query to.
.PARAMETER Guid
The software GUID you'e like to limit the query to
#>
[OutputType([System.Management.Automation.PSObject])]
[CmdletBinding()]
param (
[string]$Name,
[string]$Guid
)
process
{
try
{
$UninstallKeys = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS | Out-Null
$UninstallKeys += Get-ChildItem HKU: -ErrorAction SilentlyContinue | Where-Object { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+$' } | ForEach-Object { "HKU:\$($_.PSChildName)\Software\Microsoft\Windows\CurrentVersion\Uninstall" }
if (-not $UninstallKeys)
{
Write-Log -Message 'No software registry keys found' -LogLevel '2'
}
else
{
foreach ($UninstallKey in $UninstallKeys)
{
$friendlyNames = @{
'DisplayName' = 'Name'
'DisplayVersion' = 'Version'
}
if ($PSBoundParameters.ContainsKey('Name'))
{
$WhereBlock = { $_.GetValue('DisplayName') -like "$Name*" }
}
elseif ($PSBoundParameters.ContainsKey('GUID'))
{
$WhereBlock = { $_.PsChildName -eq $Guid }
}
else
{
$WhereBlock = { $_.GetValue('DisplayName') }
}
$SwKeys = Get-ChildItem -Path $UninstallKey -ErrorAction SilentlyContinue | Where-Object $WhereBlock
foreach ($SwKey in $SwKeys)
{
try {
$output = @{ }
foreach ($ValName in $SwKey.GetValueNames() | Where-Object { $_ })
{
if ($ValName -ne 'Version')
{
Write-Verbose -Message $ValName
$output.InstallLocation = ''
if ($ValName -eq 'InstallLocation' -and ($SwKey.GetValue($ValName)) -and (@('C:', 'C:\Windows', 'C:\Windows\System32', 'C:\Windows\SysWOW64') -notcontains $SwKey.GetValue($ValName).TrimEnd('\')))
{
$output.InstallLocation = $SwKey.GetValue($ValName).TrimEnd('\')
}
[string]$ValData = $SwKey.GetValue($ValName)
if ($friendlyNames[$ValName])
{
$output[$friendlyNames[$ValName]] = $ValData.Trim() ## Some registry values have trailing spaces.
}
else
{
$output[$ValName] = $ValData.Trim() ## Some registry values trailing spaces
}
}
}
$output.GUID = ''
if ($SwKey.PSChildName -match '\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b')
{
$output.GUID = $SwKey.PSChildName
}
New-Object –TypeName PSObject -Property $output
} catch {
Write-Log -Message $_.Exception.Message -LogLevel '2'
}
}
}
}
}
catch
{
Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
$PSCmdlet.ThrowTerminatingError($_)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment