PowerShell Check If Software Is Installed

PowerShell check if software is installed

Using Get-Package with Select-Object Cmdlet

Use Get-Package with the Select-Object cmdlet to check if the specified software is installed.

First, we declared and initialized a variable called $software having a software’s complete name that we wanted to check.

Next, we used the Get-Package cmdlet to retrieve a list of all packages installed on our local computer; all these packages were piped to the Where-Object cmdlet which checked whether the Name property is equal to the value of $software variable; if it is then, piped the result to the Select-Object cmdlet, which selected the specified $software from them and returned them; otherwise, didn’t display anything.

We can also run the Get-Package cmdlet on remote machines by executing it as a part of the Invoke-Command cmdlet or Enter-PSSession cmdlet or script.

We can also check all versions of a particular software provided by a provider. We use the Get-Package cmdlet with -ProviderName and -AllVersions parameters.

We have already learned about Get-Package; here, we used this cmdlet with the -ProviderName parameter to mention the provider’s name, which was msi in the above example. Next, we used the -Name parameter to mention the software’s name; however, the wildcard operator represented with * was used to grab all the software starting with the word Python.

Next, we used the -AllVersions parameter to retrieve all software versions installed on the local machine. By default, the Get-Package cmdlet returns the latest available version. To list a particular software version, we use -RequiredVersion followed by the version number. See the following example for a clear understanding.

If you want to know which packages (software) are installed on your local computer, we just run the Get-Package cmdlet as demonstrated below. It will display the software’s Name, Version, Source, and ProviderName; see the following output.

There can be a possibility where you have to find out the installation location of the specified software; in that case, we use the Get-Package with the Select-Object cmdlet as follows.

We have already learned about Get-Package and Select-Object; the new thing is the -ExpandProperty parameter which we used to specify the property we wanted to select. So, for example, in the above code fence, we selected the source property to know the installation location of the specified $software.

Using Get-WmiObject with Select-Object Cmdlet

Use Get-WmiObject with the Select-Object cmdlet to check if the specified software is installed.

This example is similar to the first example in the previous section, but we used the Get-WmiObject cmdlet with the -Class parameter for the above code snippet. This cmdlet retrieved all the instances of the WMI (Windows Management Instrumentation) class or information about the available classes.

Using this cmdlet as Get-WmiObject -Class Win32_Product returned a list of all the installed software on the Windows system using WMI. We can run this command on any Windows system with PowerShell installed and get details about all the installed packages, including the software’s name, vendor, version, and other important information.

Remember, using Get-WmiObject can be slow and resource-intensive, particularly when we have a system with many installed software/packages. Why is it slow? It is because it has to query the WMI database for every installed software. Further, few antivirus software may identify it as a malicious command, as the malware frequently uses it to collect information about the installed packages on a particular system. You must be careful using this and only on trusted systems.

Now, let’s modify the above code as demonstrated below.

Here, we initialize the $resultvariable with the value returned by the Get-WmiObject command. Next, we used the if statement with the -ne operator to check if the $result is not equal to $null; if it is not, we used Write-Host to display a customized message informing the specified software is installed; otherwise, the else block will be executed.

Now we know the Get-WmiObject cmdlet, why we use it, and what it returns. Let’s use another code example but with function this time.

We got Vendor, Version, Caption, Name, and IdentifyingNumber because we used * in SQL SELECT command. So we can get the same information for all the installed packages as follows.

Using Get-ChildItem Cmdlet

To check all the installed software in PowerShell:

  • Use the Get-ChildItem cmdlet to query the registry to retrieve a list of all the installed packages (software).
  • Use the ForEach loop to iterate over the list of installed packages.
  • In each iteration:
    • Use Write-Host thrice to show the DisplayName of the software, a separator, and the DisplayVersion.

We used Get-ChildItem to query the registry and get all the installed software on the local machine. We stored this list in the $installedSoftware variable, which we used with the ForEach loop. The loop iterated over the $installedSoftware, and we used the Write-Host cmdlet thrice for every element in the list.

First, Write-Host was used to print the DisplayName of the current software retrieved using the GetValue() method. The second Write-Host cmdlet was used to print a separator; we used -; you can use any of your choices. Third, Write-Host printed the DisplayVersion of the current software, which we got using using GetValue() method. Finally, the -NoNewLine was used not to insert a line break.

The above code lists all the software installed on our local machine, but the applications can be installed per user too. So, to get a list of software for a currently logged-in user, we have to change the HKLM to HKCU as demonstrated below. So, here, LM means Local Machine while CU means Current User.

To get information in tabular form, we can use the following solution.

We used Get-ItemProperty cmdlet to retrieve the DisplayName, DisplayVersion, Publisher, InstallDate properties of the selected objects. In contrast Format-Table cmdlet was used to display the received data in a tabular format.

That’s all about how to check if Software is installed.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *