Elias
Elias

Reputation: 89

Get installed software product context using powershell

I can easily get all installed software products on a machine using

Get-WmiObject -Class Win32_Product

Now I'd like to also fetch the Product Context. How can I access this information for every installed product using PowerShell.

In VB I did that by using the WindowsInstaller COM-Object and then querying the information. In essence this:

Set Com = CreateObject('WindowsInstaller.Installer')
Set Products = Com.ProductsEx(vbNullString,"S-1-1-0",7)
For Each P in Products
  context = P.Context

Which I dont not manage to replicate in PowerShell

Upvotes: 0

Views: 437

Answers (2)

bixb0012
bixb0012

Reputation: 159

I realize this question is a bit stale, but I disagree with what seems to be the prevailing notion that working with Windows Installer in PowerShell is somehow a "pain" and more complicated than working with it in VBScript (this post is just one of many).

I have found that VBScript Windows Installer code translates quite literally to PowerShell, which means there are numerous examples of VBScript Windows Installer scripts that can be adapted to PowerShell and used to learn how to work with Windows Installer in PowerShell.

For this specific question of install context, the PowerShell code is quite similar to the VB code the OP gave.

# code must be run with admin rights to use "S-1-1-0" SID
enum InstallContext {
    FirstVisible   =  0   # product visible to the current user
    None           =  0   # Invalid context for a product
    UserManaged    =  1   # user managed install context
    UserUnmanaged  =  2   # user non-managed context
    Machine        =  4   # per-machine context
    All            =  7   # All contexts. OR of all valid values
    AllUserManaged =  8   # all user-managed contexts
}

$Installer = New-Object -ComObject WindowsInstaller.Installer
foreach ($P in $Installer.ProductsEx("", "S-1-1-0", 7)) {
    [InstallContext]$P.Context()
}

NOTE: I used Enums (about Enum - PowerShell | Microsoft Docs) with PowerShell here since tagMSIINSTALLCONTEXT is an enum in the msi.h file.

Upvotes: 1

js2010
js2010

Reputation: 27516

It's a pain to use that com object in powershell. I would use vbscript instead and save the text output to a powershell variable, or find an msi powershell module. That com object doesn't have a "type library" or support "IDispatch". The Windows Powershell in Action appendix for 2nd edition goes into it, but even there it's not pretty. That vbscript code has errors.

Upvotes: 0

Related Questions