PowerShell Output types and options

This topic contains 4 replies, has 3 voices, and was last updated by  Sam Boutros 2 years, 5 months ago.

  • Author
  • #24031

    William Parry

    Hi Guys,

    I've just spent ages trying to find my own posts so I could check for any answers to this question, only to discover I hadn't actually posted this yet. 😳

    So, with standard cmdlets etc e.g. Get-VM you get something like this:

    Name               PowerState    Num CPUs    MemoryGB
    HOSTNAME    PoweredOn    4                    12.000

    but if you run it and pipe to format-list e.g. Get-VM | fl then you get something like this:

    Name                                                                         : HOSTNAME
    PowerState                                                                   : PoweredOn
    NumCpu                                                                       : 4
                               "{0:N3}" -f $_.MemoryGB
                             : 12.000
    HardDisks                                                                    : {Hard disk 1, Hard disk 2, Hard disk 3}
    CDDrives                                                                     : {CD/DVD drive 1}
    FloppyDrives                                                                 : {}
    NetworkAdapters                                                              : {Network adapter 1}
    UsbDevices                                                                   : {}
    Host                                                                         : hostname.example.com
    Id                                                                           : VirtualMachine-vm-26
    Notes                                                                        : vCenter Server 5.5

    So I've written (hacked) a script to get information about vm resets or snapshot creation etc. and I return an object to allow me to effectively use the pipeline, but I can't see how to emulate the behaviour above. Either I format the output and I get a fixed output or I output the object and all properties are returned by default as a list.

    What I'd like is to have a full list of properties like :


    Choose default output properties e.g. UserName,EntityName,Description,CompleteTime which I think will default as a table output or the option of piping the output to format-list or select-object and get more properties from the object.

    Does that all make sense and can someone tell me how to do this? I feel like this should be a standard option and perhaps I just haven't found the syntax yet. I've watched the MVA Basic Level PowerShell 3.0 JumpStart and have completed at least half of the Advanced Tools & Scripting, but haven't yet seen this yet.

  • #24034

    Don Jones

    The default output displayed by PowerShell can be determined by a type extension or formatting file, meaning that the default output can be "trimmed down" to whatever the developer thought was "most interesting." Piping to [b]Select *[/b] or [b]Format-List *[/b] overrides that behavior and shows everything. The object output by the command is always complete; it's just the display-to-screen process where it gets any default view applied.

    If you output a custom object, then the shell won't have any defaults, so it'll display everything by default. Whether it uses a list or table depends on how many properties there are – 5 or more, you get a list by default.

    If you want to modify the default display for an existing object type, or for a custom object type, then you have to get into defining a default custom view in a .ps1xml file. [i]Learn PowerShell Toolmaking in a Month of Lunches[/i] covers doing so, but it's definitely an advanced topic that has a lot of pre-requisites. I'd suggest grabbing [i]Learn PowerShell in a Month of Lunches[/i] to start with. The first 5-6 chapters are essentially working you through those core concepts.

    • #24042

      William Parry

      Nice plug 😉 I'd certainly like to take a look at that book. I'll have a look in my local library 😛

  • #24061

    William Parry

    On a more serious note, thanks for the tip, which gave me enough explanation to understand what's happening and why I wasn't getting what I wanted. Hopefully I can find a deal to get both your books as they seem to come highly recommended and would probably help me to understand PowerShell better and use it to the MAX!

    Odd that Amazon suggests buying the first edition of "Learn Windows PowerShell in a Month of Lunches" along with "Learn PowerShell Toolmaking in a Month of Lunches" and the second edition of "Learn Windows PowerShell in a Month of Lunches".

    Unless you're a serious book collector, is there any reason to buy a copy of the first edition?

  • #24069

    Sam Boutros


    I don't have access to VMWare environment, but in Hyper-V 2012 R2, Get-VM returns a different set of properties.

    From the notes you posted, I see the VMWare Get-VM cmdlet returns a complex object. That's an object, that has other objects as one or more of its properties. To get the properties you want, you can create a PS Custom object and pick the properties needed from the original object or its sub-objects. To help you further, I need Get-Member cmdlet output of the objects that have the properties you're after. For example output of

    Get-VM | GM

You must be logged in to reply to this topic.