Santiago Squarzon offers good advice, via this blog post:
The use of the Win32_Product
WMI class is best avoided, because it is incomplete, slow, and can have side effects.
See the linked blog post for a comprehensive - but cumbersome - registry-based solution that covers all users that have installed programs on a given machine.
For a convenient, but limited alternative via Get-Package
, see the bottom section.
Separately, regarding the use of Get-WmiObject
:
- The CIM cmdlets (e.g.,
Get-CimInstance
) superseded the WMI cmdlets (e.g., Get-WmiObject
) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) 7+, where all future effort will go, doesn't even have them anymore.
Note that WMI still underlies the CIM cmdlets, however. For more information, see this answer.
The answer to your question as asked:
Use Get-CimInstance
in lieu of Get-WmiObject
, for the reasons explained above.
For brevity, use a -Filter
argument
- Note: A
-Filter
argument translates into an SQL query behind the scenes, which means that the results are filtered at the source, which normally also has performance benefits, but not in the case at hand, as explained here.
Add-Type -AssemblyName Microsoft.VisualBasic
$pname = [Microsoft.VisualBasic.Interaction]::InputBox('Program Name:')
# Pipe the results to Out-File as needed.
Get-CimInstance Win32_Product -Filter "Name LIKE '$pname%'"
A Windows PowerShell alternative,[1] using Get-Package
:
Add-Type -AssemblyName Microsoft.VisualBasic
$pname = [Microsoft.VisualBasic.Interaction]::InputBox('Program Name:')
# Pipe the results to Out-File as needed.
Get-Package -ProviderName msi, Programs -Name "$pname*"
This is limited in the sense that, among the programs installed at a user level, only the current user's programs are included.
(Note that querying the Win32_Product
class is limited to MSI-installed programs (programs installed by Windows Installer) - I'm unclear on whether that includes user-level programs across all users.)
Also note that the output objects are of a different type than those returned by Get-CimInstance Win32_Product
, with different properties.
Pipe output to Get-Member -Type Properties
to discover available properties.
[1] Unfortunately, in PowerShell (Core) 7+, as of v7.3.4, the msi
and Programs
provider are not available, and it's unclear if they'll ever be.