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?
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.
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.