locked
Cmdlet's output RRS feed

  • Question

  • Hello!

    Would anyone please explain why does PS return all fields in the output, even when I explicitly request only some of them?

    Get-CimInstance -Query "SELECT DeviceID, FreeSpace FROM Win32_LogicalDisk where DriveType <=4" -Namespace root\cimv2

    Thank you in advance,
    Michael

    Friday, January 31, 2020 8:37 AM

Answers

  • Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType <=4" | 
        Select-Object -Property DeviceID, FreeSpace

    That would be the proper way to get what you want.

    For the future: How to Use the Code Feature in a TechNet Forum Post

    and ... An image of your code is not helpful


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by Richard MuellerMVP Friday, January 31, 2020 1:00 PM
    • Marked as answer by MF47 Monday, February 3, 2020 10:21 AM
    Friday, January 31, 2020 10:02 AM
  • Many PowerShell cmdlets have default properties that are returned whether or not you ask for them. Other properties are extended, meaning they are only returned if you request them. See the description of the -Property parameter in this documentation:

    https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by Just Karl Friday, January 31, 2020 9:20 PM
    • Marked as answer by MF47 Monday, February 3, 2020 10:21 AM
    Friday, January 31, 2020 2:47 PM
  • - I specified a query but got the output that does not correspond to the query. Anyway, what's the purpose of specifying any query if the output will be defined by PS itself?
    You may ask this the Powershell team. Did you check if this still exists in the newer version like 6.2.x or 7.0.0.rc2?  If yes and you consider this to be a bug you may create a new issue on the according Github repository.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by MF47 Monday, February 3, 2020 10:20 AM
    Friday, January 31, 2020 3:59 PM
  • "Other properties are extended, meaning they are only returned if you request them... See the description of the -Property parameter in this documentation" - I think it does not apply to the query inside the CimInstance cmdlet, it's my understanding that in this case it is the -Query parameter that defines what must be returned by the cmdlet:

    "-Query

    Specifies a query to run on the CIM server."

    - I specified a query but got the output that does not correspond to the query. Anyway, what's the purpose of specifying any query if the output will be defined by PS itself?

    The query is a shortcut to adding a filter and other extended CQL/WQL syntaxes.  The returned properties are always all of the properties of theobject instance.  The properties (fields) specified in the query are only added to the displayed output but all standard properties will always be displayed.  This is really a function of the default PS output formatter for the object as defined in the ps1.xml format file and is documented in the basic PS documentation and taught as a basic part of any PowerShell tutorial, book or course.  This is one reason I constantly stress that PS learners should take a course or get a book on PowerShell.  These questions are all answered in the training and are a fundamental part of PS that anyone using PS must know.


    \_(ツ)_/

    • Marked as answer by MF47 Monday, February 3, 2020 10:20 AM
    Friday, January 31, 2020 8:24 PM

All replies

  • Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType <=4" | 
        Select-Object -Property DeviceID, FreeSpace

    That would be the proper way to get what you want.

    For the future: How to Use the Code Feature in a TechNet Forum Post

    and ... An image of your code is not helpful


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by Richard MuellerMVP Friday, January 31, 2020 1:00 PM
    • Marked as answer by MF47 Monday, February 3, 2020 10:21 AM
    Friday, January 31, 2020 10:02 AM
  • BOfH-66, thank you for your help!

    Please excuse me if I've formulated my question not clearly: I'd like to know why PS outputs the fields not requested in the quiery, for example

    Get-CimInstance -Query "SELECT DeviceID, FreeSpace FROM Win32_LogicalDisk" will output ALL the fields located between DeviceID and and FreeSpace.

    Regarding the "That would be the proper way to get what you want." - I know that it would solve the problem, but why should I use the extra cmdlet (Select-Object ) if I should be able to get exactly what I need using the Get-CimInstance alone? By the way, this query (SELECT DeviceID, FreeSpace FROM Win32_LogicalDisk) returns only two fields in WMI Explorer - why does PS "behaves" differently?

    Regards,
    Michael


    Friday, January 31, 2020 2:40 PM
  • Many PowerShell cmdlets have default properties that are returned whether or not you ask for them. Other properties are extended, meaning they are only returned if you request them. See the description of the -Property parameter in this documentation:

    https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by Just Karl Friday, January 31, 2020 9:20 PM
    • Marked as answer by MF47 Monday, February 3, 2020 10:21 AM
    Friday, January 31, 2020 2:47 PM
  • "Other properties are extended, meaning they are only returned if you request them... See the description of the -Property parameter in this documentation" - I think it does not apply to the query inside the CimInstance cmdlet, it's my understanding that in this case it is the -Query parameter that defines what must be returned by the cmdlet:

    "-Query

    Specifies a query to run on the CIM server."

    - I specified a query but got the output that does not correspond to the query. Anyway, what's the purpose of specifying any query if the output will be defined by PS itself?

    Friday, January 31, 2020 3:05 PM
  • - I specified a query but got the output that does not correspond to the query. Anyway, what's the purpose of specifying any query if the output will be defined by PS itself?
    You may ask this the Powershell team. Did you check if this still exists in the newer version like 6.2.x or 7.0.0.rc2?  If yes and you consider this to be a bug you may create a new issue on the according Github repository.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by MF47 Monday, February 3, 2020 10:20 AM
    Friday, January 31, 2020 3:59 PM
  • "Other properties are extended, meaning they are only returned if you request them... See the description of the -Property parameter in this documentation" - I think it does not apply to the query inside the CimInstance cmdlet, it's my understanding that in this case it is the -Query parameter that defines what must be returned by the cmdlet:

    "-Query

    Specifies a query to run on the CIM server."

    - I specified a query but got the output that does not correspond to the query. Anyway, what's the purpose of specifying any query if the output will be defined by PS itself?

    The query is a shortcut to adding a filter and other extended CQL/WQL syntaxes.  The returned properties are always all of the properties of theobject instance.  The properties (fields) specified in the query are only added to the displayed output but all standard properties will always be displayed.  This is really a function of the default PS output formatter for the object as defined in the ps1.xml format file and is documented in the basic PS documentation and taught as a basic part of any PowerShell tutorial, book or course.  This is one reason I constantly stress that PS learners should take a course or get a book on PowerShell.  These questions are all answered in the training and are a fundamental part of PS that anyone using PS must know.


    \_(ツ)_/

    • Marked as answer by MF47 Monday, February 3, 2020 10:20 AM
    Friday, January 31, 2020 8:24 PM
  • "Did you check if this still exists in the newer version like 6.2.x or 7.0.0.rc2?" - no, I'm using 5.1 version.

    "This is really a function of the default PS output formatter for the object as defined in the ps1.xml format file and is documented in the basic PS documentation and taught as a basic part of any PowerShell tutorial, book or course.  This is one reason I constantly stress that PS learners should take a course or get a book on PowerShell. " - I've already learned a textbook on Powershell and it really did explain the purpose "of the default PS output formatter for the object as defined in the ps1.xml format file" but I never thought that the default formater would apply to the SQL-like commands. And I have at least one reason to doubt that the default formater is always applied: I've seen many times - not in this particular command mentioned above - that NOT all properties are always displayed for the object instance. When working with WMI data I sometimes had to select the fields Field5, Field6, Field7  (out of 10) instead of Field1, Field6 and Field7 (if, for example, the Field1 and Field5 both displays the same drive's name - C:, D:, ...) -in this case the non-requested fields 2-3-4 did not appear in the output. In other words the "extra" fields were displayed only when they were between the requested fields (between Field1 and Field5 in my example). The fields behind the last-requested field - Field7 - (Field8-Field10) were never displayed at all.

    Thank you all for your help!

    Regards,
    Michael

    Monday, February 3, 2020 10:20 AM
  • All Objects are defined in a file or by defaulting methods.  You can search the files for the object name to see what is applied.

    Also WMI always returns all default properties and the WQL field list is just used to add non-default properties.  THis is why using a filter and the class is the normal way of coding.  Since WMI can be used to query non-Windows systems when the WMI service is added the fields may be more appicable.

    To see all fields returned just do this:

    Get-CimInstance Win32_Bios

    Now do this:

    Get-CimInstance t Win32_Bios | select *

    Note that all fields have actually been returned.

    Now try this:

    Get-CimInstance  Win32_Bios -Property *

    Now do this:

    Get-CimInstances -query 'select * from Win32_Bios' | select *

    If you look at the format files you will see how this magic is accomplished.


    \_(ツ)_/

    Monday, February 3, 2020 11:13 AM
  • Thank you, jrv!
    Tuesday, February 4, 2020 1:40 PM