Detecting Installed Applications with PowerShell
In this blog post, we'll explore a PowerShell script that detects installed applications on a Windows system and checks if there are any updates available. This script uses the Win32_Product WMI class to gather information about installed products, and then utilizes the Winget command-line utility to search for updates.
Prerequisites
The Script
The script begins by clearing the console host and initializing an empty array called `$updateable`. It then uses the `Get-WmiObject` cmdlet to retrieve a list of installed products, sorted by name and version:
clear-host
$updateable = @()
$appslist = Get-WmiObject -Class Win32_Product | Select-Object Name, Vendor, IdentifyingNumber, Version | sort-object Name, Version
The script then loops through each application in the `$appslist`, creating a custom object for each app:
foreach ($app in $appslist) {
$a = [PScustomObject]@{
Name = $app.name
Vendor = $app.Vendor
ID = $app.identifyingNumber
Version = $app.Version
}
Within the loop, the script searches for each application in Winget using the `Invoke-WingetList` function. If an update is found, it's added to the `$updateable` array:
if ($null -ne $($a.name)) {
Write-host "🔍 Searching for $($a.name) in Winget" -ForegroundColor Yellow
$output = Invoke-WingetList -Name $a.name | Where-Object {$_.Available -ne $Null} | Select-Object NamePrefix, IDprefix, Version, Available, Source
if ($null -ne $($output.Nameprefix)) {
Write-host "`t$($output.Nameprefix) new version $($output.Available) found in Winget" -ForegroundColor Cyan
$updateable += $output
}
}
How It Works
WMI and Win32_Product Class
The script uses the `Get-WmiObject` cmdlet to retrieve a list of installed products from the Win32_Product WMI class. This class provides information about installed software, including product names, vendors, and versions.
Custom Objects and Looping
The script loops through each application in the `$appslist`, creating a custom object for each app using the `[PScustomObject]` type accelerator. This allows us to easily access and manipulate properties like `Name`, `Vendor`, `ID`, and `Version`.
Key Code Snippets
$updateable | Sort-Object Nameprefix | Format-Table -AutoSize
if ($updateable -gt $null) {
Write-host "updates available for $($updateable.count) applications"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#start-process -filepath powershell.exe -argumentlist 'winget upgrade --all' -wait -verbose -Verb runas
Usage Examples
To use this script, simply copy and paste it into your PowerShell console or save it to a file and run it. The script will output the names of any applications that have updates available.
Conclusion
This script demonstrates how PowerShell can be used to detect installed applications and check for updates. By leveraging WMI, custom objects, and Winget, this script provides a useful tool for managing software updates on Windows systems.