'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 1:[1]
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 forGet-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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |