Last active
July 24, 2025 09:12
-
-
Save HighLibrarian/ac2ea19856fd9cd3f4168ba42dd8829f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Retrieves information about installed applications based on different search modes. | |
.DESCRIPTION | |
The Get-InstalledApps function retrieves information about installed applications based on various search modes | |
such as Registry, CmCIM, or File. It allows querying by application name, version, and search mode. | |
.PARAMETER Appname | |
The name of the application to search for. | |
.PARAMETER Version | |
The version of the application to search for. | |
.PARAMETER VersionOperator | |
The version comparison operator. Valid values are "EQ" (equal) and "GE" (greater than or equal). Default is "GE". | |
.PARAMETER SearchMode | |
The search mode to use. Valid values are "Registry", "CmCIM", "AppX" or "File". | |
.PARAMETER FilePath | |
The file path to the application if using the "File" search mode. | |
.EXAMPLE | |
Get-InstalledApps -Appname "FortiClient" -Version "7.2.4.09" -SearchMode CmCIM | |
Retrieves information about the "FortiClient" application with version "7.2.4.09" using the CmCIM search mode. | |
.EXAMPLE | |
Get-InstalledApps -Appname "FortiClient" -Version "7.2.4.09" -SearchMode Registry -VersionOperator GE | |
Retrieves information about installed instances of the "FortiClient" application with version greater than or equal to "7.2.4.09" using the Registry search mode. | |
.EXAMPLE | |
Get-InstalledApps -Appname "FortiClient" -Version "7.2.4.09" -SearchMode File -FilePath "C:\Program Files\Fortinet\FortiClient\FortiClient.exe" -VersionOperator EQ | |
Retrieves information about the "FortiClient" application with version "7.2.4.09" located at the specified file path using the File search mode. | |
#> | |
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, ParameterSetName ='detect')] | |
[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