3

I've been learning PowerShell over the past month and often times I've found through select statements or -properties iirc, that I get additional data that I don't ask for. Hoping someone can point out why it spits out extra data.

Example:

Get-WmiObject -query "Select name,vendor,version from win32_product where name = 'Service Name'" -property name,vendor,version

Result:

__GENUS          : 2
__CLASS          : Win32_Product
__SUPERCLASS     : 
__DYNASTY        : 
__RELPATH        : 
__PROPERTY_COUNT : 3
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
Name             : <name stuff>
Vendor           : <vendor stuff>
Version          : <version number stuff>
PSComputerName   : 

Why is it giving me all these additional fields when I'm specifying only 3?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Ayyub
  • 33
  • 3
  • that cmdlet apparently does not do what i [and you] expect with that parameter. from the docs ... >>> `Specifies the WMI class properties that this cmdlet gets information from. Enter the property names.` <<< note that it does not say ONLY those properties will be returned. i don't know what that parameter really does, tho. [*blush*] ///// the parameter works as expected with other most cmdlets, tho. – Lee_Dailey Apr 16 '22 at 21:21
  • Precisely. That was my other instance where it was outputting more than expected. Thought I was the only one. I have to pipe it to select to get the results I'd like. – Ayyub Apr 16 '22 at 22:00
  • 1
    yep ... build/pick your desired props AFTER the fact is the only way that i can find. i sometimes prefer to use `ForEach-Object` and `[PSCustomObject]` to build things the way that i want, tho. [*grin*] – Lee_Dailey Apr 16 '22 at 22:02
  • 2
    As an aside: 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) v6+, 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](https://stackoverflow.com/a/54508009/45375). – mklement0 Apr 16 '22 at 23:31
  • 1
    Thanks @mklement0 I did not know, I'm scraping together what I can to learn more when I need to resolve something so it's good to know. – Ayyub Apr 17 '22 at 00:00
  • As an aside: you cannot combine `-Property` with `-Query`: Query forces you to select the output columns (properties) as part of the query, so there's no need to also specify them via `-Property`. Conversely, if you use a `-Filter` argument (essentially, the WHERE clause of a WQL statement), `*` is implied (all columns), and in this case you can use `-Property` to select the desired columns (properties). – mklement0 Apr 17 '22 at 14:15

1 Answers1

2

First the obligatory reminder:

  • 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) v6+, 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.

  • Therefore, I'm using Get-CimInstance below; substituting it for Get-WmiObject will typically work, but there are some basic differences - see the linked answer.

It is easier to let PowerShell extract the property values of interest after the fact, using Select-Object:

Get-CimInstance -Class Win32_Product -Filter  "Name = 'Service Name'" | 
  Select-Object -Property name, vendor, version

Using Select-Object ensures that the output object(s) have the specified properties only, and not also the __-prefixed properties that Get-CimInstance's output objects are decorated with (in addition to other generic properties that aren't displayed by default).

Note: I doubt that it is required for performance reasons, but you can limit property retrieval at the source as well:

$props = 'name', 'vendor', 'version'
Get-CimInstance -Class Win32_Product -Filter  "Name = 'Service Name'"  -Property $props | 
  Select-Object -Property $props

mklement0
  • 382,024
  • 64
  • 607
  • 775