Thursday, 8 February 2018

Check if a Software Program Is Installed using PowerShell Script

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.


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;

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'


No comments:

Post a Comment