views:

60

answers:

1

I'd like to do this using the win32_product wmi class.

I need the script to simply count the number of products installed and also output the time taken to execute the script.

What I have atm doesn't seem to work correctly:

    $count = 0
    $products = get-wmiobject -class "Win32_Product"
    foreach ($product in $products) {
          if ($product.InstallState -eq 5) {
                count++
          }
    }
    write-host count
+1  A: 

Roman Kuzmin is right about the typo. The will solve almost everything.

To make it more powershellish, I would use

get-wmiobject -class "Win32_Product" | 
    ? { $_.InstallState -eq 5 } |
    measure-object  | 
    select -exp Count

And considering the time, you can wrap it into measure-command

measure-command { 
  $count = get-wmiobject -class "Win32_Product" | 
              ? { $_.InstallState -eq 5 } | 
              measure-object  |
              select -exp Count
  write-host Count: $count
}
stej