Skip to content

Instantly share code, notes, and snippets.

@HighLibrarian
Last active March 4, 2025 13:24
Show Gist options
  • Save HighLibrarian/ac2ea19856fd9cd3f4168ba42dd8829f to your computer and use it in GitHub Desktop.
Save HighLibrarian/ac2ea19856fd9cd3f4168ba42dd8829f to your computer and use it in GitHub Desktop.
function Get-InstalledApps
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $false, ParameterSetName ='list')]
[switch]$List,
[Parameter(Mandatory = $false, ParameterSetName ='detect')]
[string]$Appname,
[Parameter(Mandatory = $false, ParameterSetName ='detect')]
[string]$Version,
[Parameter(Mandatory = $false, ParameterSetName ='detect')]
[validateset("EQ","GE")]
[string]$VersionOperator = "GE",
[Parameter(Mandatory = $false, ParameterSetName ='list')]
[Parameter(Mandatory = $true, ParameterSetName ='detect')]
[validateset("Registry","CmCIM", "File","Appx")]
[string]$SearchMode,
[Parameter(Mandatory = $false)]
[string]$FilePath,
# Exact name matching
[Parameter(Mandatory = $false, ParameterSetName ='detect')]
[switch]$ExactName
)
begin
{
switch ($SearchMode)
{
"Registry"
{
$AppsFound = @()
$x86Apps = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object Displayname, Displayversion, Publisher, installdate, UninstallString
$x64Apps = Get-ItemProperty -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object Displayname, Displayversion, Publisher, installdate, UninstallString
$AppsFound += $x86Apps
$AppsFound += $x64Apps
}
"CmCIM"
{
$AppsFound = get-CimInstance -Namespace root/cimv2/sms -ClassName SMS_InstalledSoftware |
Select-Object @{Label='Displayname'; Expression = {$_.ARPDisplayName}},
@{Label='DisplayVersion'; Expression = {$_.ProductVersion}},
Publisher, InstallDate, UninstallString
}
"File"
{
$AppsFound = Get-ItemProperty -Path $FilePath -ErrorAction Ignore |
Select-Object @{Label='Displayname'; Expression = {$_.BaseName}},
@{Label='DisplayVersion'; Expression = {$_.VersionInfo.FileVersion}}
}
"Appx"
{
$AppsFound = Get-AppxPackage -AllUsers |
Select-Object @{Label='Displayname'; Expression = {$_.Name}},
@{Label='DisplayVersion'; Expression = {$_.Version}},
Publisher
}
Default
{
$AppsFound = @()
$x86Apps = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object Displayname, Displayversion, Publisher, installdate, UninstallString
$x64Apps = Get-ItemProperty -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object Displayname, Displayversion, Publisher, installdate, UninstallString
$AppsFound += $x86Apps
$AppsFound += $x64Apps
}
}
}
process
{
if (-not $List)
{
if ($ExactName)
{
# Perform exact match
$AppInfo = $AppsFound | Where-Object { $_.Displayname -eq $Appname }
}
else
{
# Perform partial match
$AppInfo = $AppsFound | Where-Object { $_.Displayname -match [regex]::Escape($Appname) }
}
if ($AppInfo)
{
$AppVersionFound = $AppInfo.DisplayVersion -as [string]
$AppVersionSearch = $Version -as [string]
$IsNumericVersion = $AppVersionSearch -match "^\d+(\.\d+)*$"
if ($IsNumericVersion)
{
try
{
$AppVersionFound = [version]$AppVersionFound
$AppVersionSearch = [version]$AppVersionSearch
switch ($VersionOperator)
{
"GE" { $Appfound = $AppVersionFound -ge $AppVersionSearch }
"EQ" { $Appfound = $AppVersionFound -eq $AppVersionSearch }
}
}
catch
{
$Appfound = $false
}
}
else
{
if ($AppVersionFound -match [regex]::Escape($AppVersionSearch))
{
$Appfound = $true
}
else
{
$Appfound = $false
}
}
}
else
{
$Appfound = $false
}
}
}
end
{
if ($List)
{
$AppToDetect = $AppsFound | Out-GridView -Title "Select the app you want to detect:" -PassThru
Write-Host "Get-InstalledApps -Appname `"$($AppToDetect.Displayname)`" -Version `"$($AppToDetect.Displayversion)`" -SearchMode $SearchMode"
}
else
{
if ($Appfound -eq $true)
{
Write-Host "Installed"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment