I'm writing a powershell script that will install some dependencies for my webapp. In my script, I'm running into a recurring problem of checking if a particular application is installed. it seems there's a unique way of checking if an application exists for each application (ie: by checking the existing of this folder or this file on c:). Is there not a way that i can check if an application is installed by querying a list of installed applications?
The Windows Installer MSI options can be found by opening a command window (click the Windows Start button, type in cmd and press ENTER on the keyboard), and in the command window typing msiexec /? and pressing ENTER on the keyboard.
To see useful names of msi files in C:\Windows\Installer right click at the top of explorer and add the field to view Subject (will probably have to select more as it isn't a default like name, date modified, etc.) From here you can find the msi and use it to uninstall programs. Save this answer.
If you have access to the MSI, then probably the easiest way to find the ProductCode is by opening an MSI editor tool and heading to the Property table. There, you will find the ProductCode property, which gives you the unique identifier of that Windows Installer package.
Here is the code I use sometimes (not too often, so...). See the help comments for details.
<#
.SYNOPSIS
Gets uninstall records from the registry.
.DESCRIPTION
This function returns information similar to the "Add or remove programs"
Windows tool. The function normally works much faster and gets some more
information.
Another way to get installed products is: Get-WmiObject Win32_Product. But
this command is usually slow and it returns only products installed by
Windows Installer.
x64 notes. 32 bit process: this function does not get installed 64 bit
products. 64 bit process: this function gets both 32 and 64 bit products.
#>
function Get-Uninstall
{
# paths: x86 and x64 registry keys are different
if ([IntPtr]::Size -eq 4) {
$path = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
}
else {
$path = @(
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
}
# get all data
Get-ItemProperty $path |
# use only with name and unistall information
.{process{ if ($_.DisplayName -and $_.UninstallString) { $_ } }} |
# select more or less common subset of properties
Select-Object DisplayName, Publisher, InstallDate, DisplayVersion, HelpLink, UninstallString |
# and finally sort by name
Sort-Object DisplayName
}
Get-Uninstall
To get a list of installed applications try:
$r = Get-WmiObject Win32_Product | Where {$_.Name -match 'Microsoft Web Deploy' }
if ($r -ne $null) { ... }
See the docs on Win32_Product for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With