powershellwql

PowerShell output returns more fields than selected


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?


Solution

  • First the obligatory reminder:

    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.

    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
    
    

    Note that a Select-Object call is still needed, because using -Property with Get-CimInstance still returns objects with all the usual properties[1], and only limits what properties get filled in.


    [1] They are of type CimInstance, adapted by PowerShell in a class-specific manner.