We can easily check the list of installed applications via Control Panel’s Add or Remove Programs UI. But if you are System Administrator and need to frequently check whether an application is installed or not, the PowerShell script will be very useful in this case.
Summary:
- Check if a Software is installed by using WMI
- Check if a Program is installed or not by checking registry
- Check if a Software is installed in Remote Machine
- Export list of Installed Software Programs into CSV file
Check if a Software is installed by using WMI query
The below function checks the application is installed or not by using Powershell’s WMI Class Win32_Product.
function Check_Program_Installed( $programName ) { $wmi_check = (Get-WMIObject -Query "SELECT * FROM Win32_Product Where Name Like '%$programName%'").Length -gt 0 return $wmi_check; } Check_Program_Installed("Microsoft SQL")
Check if a Program is installed or not by checking registry value
The below PowerShell function check the Uninstall location and returns true if a given program is installed and returns false if not installed.
function Check_Program_Installed( $programName ) { $x86_check = ((Get-ChildItem "HKLM:Software\Microsoft\Windows\CurrentVersion\Uninstall") | Where-Object { $_."Name" -like "*$programName*" } ).Length -gt 0; if(Test-Path 'HKLM:Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall') { $x64_check = ((Get-ChildItem "HKLM:Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") | Where-Object { $_."Name" -like "*$programName*" } ).Length -gt 0; } return $x86_check -or $x64_check; } Check_Program_Installed("Microsoft")
This above script checks both the regular Uninstall location as well as the”Wow6432Node” location to ensure that both 32-bit and 64-bit locations are checked for software installations.
Check if a Software is installed in Remote Machine
The below function checks if the given software program is installed or not in remote computer.
function Check_Program_Installed($computer, $programName ) { $wmi_check = (Get-WMIObject -ComputerName $computer -Query "SELECT * FROM Win32_Product Where Name Like '%$programName%'").Length -gt 0 return $wmi_check; } Check_Program_Installed("hp-pc","Microsoft SQL")
Export list of Installed Software Programs into CSV file
You can export the installed software application details to CSV using Powershell’s Export-CSV cmdlet. The following script exports the Non-Microsoft applications to CSV file.
Get-WMIObject -Query "SELECT * FROM Win32_Product Where Not Vendor Like '%Microsoft%'" | Select-Object Name,Version,Vendor,InstallLocation,InstallDate | Export-CSV 'C:\Non_MS_Products.csv'
Ive read that you SHOULD NOT query the Win32_Product class on production machines, you actually performed a consistency check of each installation. Take a look at the Application Log after running the previous command (event viewer), and you will see something similar to this:
http://support.microsoft.com/kb/974524
https://gregramsey.net/2012/02/20/win32_product-is-evil/